// function url_valid($url) { Rev:20110423_2000//// Return associative array of valid URI components, or FALSE if $url is not// RFC-3986 compliant. If the passed URL begins with: "www." or "ftp.", then// "http://" or "ftp://" is prepended and the corrected full-url is stored in// the return array with a key name "url". This value should be used by the caller.//// Return value: FALSE if $url is not valid, otherwise array of URI components:// e.g.// Given: "http://www.jmrware.com:80/articles?height=10&width=75#fragone"// Array(// [scheme] => http// [authority] => www.jmrware.com:80// [userinfo] =>// [host] => www.jmrware.com// [IP_literal] =>// [IPV6address] =>// [ls32] =>// [IPvFuture] =>// [IPv4address] =>// [regname] => www.jmrware.com// [port] => 80// [path_abempty] => /articles// [query] => height=10&width=75// [fragment] => fragone// [url] => http://www.jmrware.com:80/articles?height=10&width=75#fragone// )function url_valid($url) {if (strpos($url, 'www.') === 0) $url = 'http://'. $url;if (strpos($url, 'ftp.') === 0) $url = 'ftp://'. $url;if (!preg_match('/# Valid absolute URI having a non-empty, valid DNS host.^(?P<scheme>[A-Za-z][A-Za-z0-9+\-.]*):\/\/(?P<authority>(?:(?P<userinfo>(?:[A-Za-z0-9\-._~!$&\'()*+,;=:]|%[0-9A-Fa-f]{2})*)@)?(?P<host>(?P<IP_literal>\[(?:(?P<IPV6address>(?: (?:[0-9A-Fa-f]{1,4}:){6}| ::(?:[0-9A-Fa-f]{1,4}:){5}| (?: [0-9A-Fa-f]{1,4})?::(?:[0-9A-Fa-f]{1,4}:){4}| (?:(?:[0-9A-Fa-f]{1,4}:){0,1}[0-9A-Fa-f]{1,4})?::(?:[0-9A-Fa-f]{1,4}:){3}| (?:(?:[0-9A-Fa-f]{1,4}:){0,2}[0-9A-Fa-f]{1,4})?::(?:[0-9A-Fa-f]{1,4}:){2}| (?:(?:[0-9A-Fa-f]{1,4}:){0,3}[0-9A-Fa-f]{1,4})?:: [0-9A-Fa-f]{1,4}:| (?:(?:[0-9A-Fa-f]{1,4}:){0,4}[0-9A-Fa-f]{1,4})?::)(?P<ls32>[0-9A-Fa-f]{1,4}:[0-9A-Fa-f]{1,4}| (?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))| (?:(?:[0-9A-Fa-f]{1,4}:){0,5}[0-9A-Fa-f]{1,4})?:: [0-9A-Fa-f]{1,4}| (?:(?:[0-9A-Fa-f]{1,4}:){0,6}[0-9A-Fa-f]{1,4})?::)| (?P<IPvFuture>[Vv][0-9A-Fa-f]+\.[A-Za-z0-9\-._~!$&\'()*+,;=:]+))\])| (?P<IPv4address>(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))| (?P<regname>(?:[A-Za-z0-9\-._~!$&\'()*+,;=]|%[0-9A-Fa-f]{2})+))(?::(?P<port>[0-9]*))?)(?P<path_abempty>(?:\/(?:[A-Za-z0-9\-._~!$&\'()*+,;=:@]|%[0-9A-Fa-f]{2})*)*)(?:\?(?P<query> (?:[A-Za-z0-9\-._~!$&\'()*+,;=:@\\/?]|%[0-9A-Fa-f]{2})*))?(?:\#(?P<fragment> (?:[A-Za-z0-9\-._~!$&\'()*+,;=:@\\/?]|%[0-9A-Fa-f]{2})*))?$/mx', $url, $m)) return FALSE;switch ($m['scheme']) {case 'https':case 'http':if ($m['userinfo']) return FALSE; // HTTP scheme does not allow userinfo.break;case 'ftps':case 'ftp':break;default:return FALSE; // Unrecognized URI scheme. Default to FALSE.}// Validate host name conforms to DNS "dot-separated-parts".if ($m['regname']) { // If host regname specified, check for DNS conformance.if (!preg_match('/# HTTP DNS host name.^ # Anchor to beginning of string.(?!.{256}) # Overall host length is less than 256 chars.(?: # Group dot separated host part alternatives.[A-Za-z0-9]\. # Either a single alphanum followed by dot| # or... part has more than one char (63 chars max).[A-Za-z0-9] # Part first char is alphanum (no dash).[A-Za-z0-9\-]{0,61} # Internal chars are alphanum plus dash.[A-Za-z0-9] # Part last char is alphanum (no dash).\. # Each part followed by literal dot.)* # Zero or more parts before top level domain.(?: # Explicitly specify top level domains.com|edu|gov|int|mil|net|org|biz|info|name|pro|aero|coop|museum|asia|cat|jobs|mobi|tel|travel|[A-Za-z]{2}) # Country codes are exactly two alpha chars.\.? # Top level domain can end in a dot.$ # Anchor to end of string./ix', $m['host'])) return FALSE;}$m['url'] = $url;for ($i = 0; isset($m[$i]); ++$i) unset($m[$i]);return $m; // return TRUE == array of useful named $matches plus the valid $url.}
function validateURL(textval) {var urlregex = new RegExp("^(http|https|ftp)\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?([a-zA-Z0-9\-\._\?\,\'/\\\+&%\$#\=~])*$");return urlregex.test(textval);}
function validateURL(textval) {var urlregex = new RegExp("^(http|https|ftp)\://([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&%\$#\=~_\-]+))*$");return urlregex.test(textval);}
public static void main(args) {String url = "go to http://www.m.abut.ly/abc its awesome"url = url.replaceAll(/https?:\/\/w{0,3}\w*?\.(\w*?\.)?\w{2,3}\S*|www\.(\w*?\.)?\w*?\.\w{2,3}\S*|(\w*?\.)?\w*?\.\w{2,3}[\/\?]\S*/ , { it ->"woof${it}woof"})println url}
http://google.comhttp://google.com/help.phphttp://google.com/help.php?a=5
http://www.google.comhttp://www.google.com/help.phphttp://www.google.com?a=5
google.com?a=5google.com/help.phpgoogle.com/help.php?a=5
http://www.m.google.com/help.php?a=5 (and all its permutations)www.m.google.com/help.php?a=5 (and all its permutations)m.google.com/help.php?a=5 (and all its permutations)
String regularExpression = "((((ht{2}ps?://)?)((w{3}\\.)?))?)[^.&&[a-zA-Z0-9]][a-zA-Z0-9.-]+[^.&&[a-zA-Z0-9]](\\.[a-zA-Z]{2,3})";
assertTrue("www.google.com".matches(regularExpression));assertTrue("www.google.co.uk".matches(regularExpression));assertTrue("http://www.google.com".matches(regularExpression));assertTrue("http://www.google.co.uk".matches(regularExpression));assertTrue("https://www.google.com".matches(regularExpression));assertTrue("https://www.google.co.uk".matches(regularExpression));assertTrue("google.com".matches(regularExpression));assertTrue("google.co.uk".matches(regularExpression));assertTrue("google.mu".matches(regularExpression));assertTrue("mes.intnet.mu".matches(regularExpression));assertTrue("cse.uom.ac.mu".matches(regularExpression));
//cannot contain 2 '.' after wwwassertFalse("www..dr.google".matches(regularExpression));
//cannot contain 2 '.' just before comassertFalse("www.dr.google..com".matches(regularExpression));
// to test case where url www must be followed with a '.'assertFalse("www:google.com".matches(regularExpression));
// to test case where url www must be followed with a '.'//assertFalse("http://wwwe.google.com".matches(regularExpression));
// to test case where www must be preceded with a '.'assertFalse("https://www@.google.com".matches(regularExpression));
public static final Matcher WEB = Pattern.compile(new StringBuilder().append("((?:(http|https|Http|Https|rtsp|Rtsp):").append("\\/\\/(?:(?:[a-zA-Z0-9\\$\\-\\_\\.\\+\\!\\*\\'\\(\\)").append("\\,\\;\\?\\&\\=]|(?:\\%[a-fA-F0-9]{2})){1,64}(?:\\:(?:[a-zA-Z0-9\\$\\-\\_").append("\\.\\+\\!\\*\\'\\(\\)\\,\\;\\?\\&\\=]|(?:\\%[a-fA-F0-9]{2})){1,25})?\\@)?)?").append("((?:(?:[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}\\.)+") // named host.append("(?:") // plus top level domain.append("(?:aero|arpa|asia|a[cdefgilmnoqrstuwxz])").append("|(?:biz|b[abdefghijmnorstvwyz])").append("|(?:cat|com|coop|c[acdfghiklmnoruvxyz])").append("|d[ejkmoz]").append("|(?:edu|e[cegrstu])").append("|f[ijkmor]").append("|(?:gov|g[abdefghilmnpqrstuwy])").append("|h[kmnrtu]").append("|(?:info|int|i[delmnoqrst])").append("|(?:jobs|j[emop])").append("|k[eghimnrwyz]").append("|l[abcikrstuvy]").append("|(?:mil|mobi|museum|m[acdghklmnopqrstuvwxyz])").append("|(?:name|net|n[acefgilopruz])").append("|(?:org|om)").append("|(?:pro|p[aefghklmnrstwy])").append("|qa").append("|r[eouw]").append("|s[abcdeghijklmnortuvyz]").append("|(?:tel|travel|t[cdfghjklmnoprtvwz])").append("|u[agkmsyz]").append("|v[aceginu]").append("|w[fs]").append("|y[etu]").append("|z[amw]))").append("|(?:(?:25[0-5]|2[0-4]") // or ip address.append("[0-9]|[0-1][0-9]{2}|[1-9][0-9]|[1-9])\\.(?:25[0-5]|2[0-4][0-9]").append("|[0-1][0-9]{2}|[1-9][0-9]|[1-9]|0)\\.(?:25[0-5]|2[0-4][0-9]|[0-1]").append("[0-9]{2}|[1-9][0-9]|[1-9]|0)\\.(?:25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}").append("|[1-9][0-9]|[0-9])))").append("(?:\\:\\d{1,5})?)") // plus option port number.append("(\\/(?:(?:[a-zA-Z0-9\\;\\/\\?\\:\\@\\&\\=\\#\\~") // plus option query params.append("\\-\\.\\+\\!\\*\\'\\(\\)\\,\\_])|(?:\\%[a-fA-F0-9]{2}))*)?").append("(?:\\b|$)").toString()).matcher("");
(?(DEFINE)# Definitions(?<ALPHA>[\p{L}])(?<DIGIT>[0-9])(?<HEX>[0-9a-fA-F])(?<NCCHAR>(?&UNRESERVED)|(?&PCT_ENCODED)|(?&SUB_DELIMS)|@)(?<PCHAR>(?&UNRESERVED)|(?&PCT_ENCODED)|(?&SUB_DELIMS)|:|@|\/)(?<UCHAR>(?&UNRESERVED)|(?&PCT_ENCODED)|(?&SUB_DELIMS)|:)(?<RCHAR>(?&UNRESERVED)|(?&PCT_ENCODED)|(?&SUB_DELIMS))(?<PCT_ENCODED>%(?&HEX){2})(?<UNRESERVED>((?&ALPHA)|(?&DIGIT)|[-._~]))(?<RESERVED>(?&GEN_DELIMS)|(?&SUB_DELIMS))(?<GEN_DELIMS>[:\/?#\[\]@])(?<SUB_DELIMS>[!$&'()*+,;=])# URI Parts(?<d_scheme>(?!urn)(?:(?&ALPHA)((?&ALPHA)|(?&DIGIT)|[+-.])*(?=:)))(?<d_hier_part_slashes>(\/{2})?)(?<d_authority>(?&d_userinfo)?)(?<d_userinfo>(?&UCHAR)*)(?<d_ipv6>(?![^:]*::[^:]*::[^:]*)((((?&HEX){0,4}):){1,7}((?&d_ipv4)|:|(?&HEX){1,4})))(?<d_ipv4>((?&octet)\.){3}(?&octet))(?<octet>(25[]0-5]|2[0-4](?&DIGIT)|1(?&DIGIT){2}|[1-9](?&DIGIT)|(?&DIGIT)))(?<d_reg_name>(?&RCHAR)*)(?<d_urn_name>(?&UCHAR)*)(?<d_port>(?&DIGIT)*)(?<d_path>(\/((?&PCHAR)*)*(?=\?|\#|$)))(?<d_query>(((?&PCHAR)|\/|\?)*)?)(?<d_fragment>(((?&PCHAR)|\/|\?)*)?))^(?<link>(?:(?<scheme>(?<urn>urn)|(?&d_scheme)):)(?(urn)(?:(?<namespace_identifier>[0-9a-zA-Z\-]+):(?<namespace_specific_string>(?&d_urn_name)+))|(?<hier_part>(?<slashes>(?&d_hier_part_slashes))(?<authority>(?:(?<userinfo>(?&d_authority))@)?(?<host>(?<ipv4>\[?(?&d_ipv4)\]?)|(?<ipv6>\[(?&d_ipv6)\])|(?<domain>(?&d_reg_name)))(?::(?<port>(?&d_port)))?)(?<path>(?&d_path))?)(?:\?(?<query>(?&d_query)))?(?:\#(?<fragment>(?&d_fragment)))?))$
测试弦
# Valid URIsftp://cnn.example.com&story=breaking_news@10.0.0.1/top_story.htmftp://ftp.is.co.za/rfc/rfc1808.txthttp://www.ietf.org/rfc/rfc2396.txtldap://[2001:db8::7]/c=GB?objectClass?onemailto:John.Doe@example.comnews:comp.infosystems.www.servers.unixtel:+1-816-555-1212telnet://192.0.2.16:80/urn:isbn:0451450523urn:oid:2.16.840urn:isan:0000-0000-9E59-0000-O-0000-0000-2urn:oasis:names:specification:docbook:dtd:xml:4.1.2http://localhost/test/somefile.php?query=someval&variable=value#fragmenthttp://[2001:db8:a0b:12f0::1]/testftp://username:password@domain.com/path/to/file/somefile.html?queryVariable=value#fragmenthttps://subdomain.domain.com/path/to/file.php?query=value#fragmenthttps://subdomain.example.com/path/to/file.php?query=value#fragmentmailto:john.smith(comment)@example.commailto:user@[2001:DB8::1]mailto:user@[255:192:168:1]mailto:M.Handley@cs.ucl.ac.ukhttp://localhost:4433/path/to/file?query#fragment# Note that the example below IS a valid as it does follow RFC standardslocalhost:4433/path/to/file
# These work with the optional scheme group although I'd suggest making the scheme mandatory as misinterpretations can occurjohn.doe@gmail.comwww.google.com/pathtofile.php?query[192a:123::192.168.1.1]:80/path/to/file.html?query#fragment
function RegExForUrlMatch(){var expression = /(https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9]\.[^\s]{2,})/g;
var regex = new RegExp(expression);var t = document.getElementById("url").value;
if (t.match(regex)) {document.getElementById("demo").innerHTML = "Successful match";} else {document.getElementById("demo").innerHTML = "No match";}}
<input type="text" id="url" placeholder="url" onkeyup="RegExForUrlMatch()">
<p id="demo">Please enter a URL to test</p>
var hasURL = (str) =>{var url_pattern = new RegExp("(www.|http://|https://|ftp://)\w*");if(!url_pattern.test(str)){document.getElementById("demo").innerHTML = 'No URL';}elsedocument.getElementById("demo").innerHTML = 'String has a URL';};
<p>Please enter a string and test it has any url or not</p><input type="text" id="url" placeholder="url" onkeyup="hasURL(document.getElementById('url').value)"><p id="demo"></p>
/**** The URL() constructor returns a newly created URL object representing* the URL defined by the parameters.** https://developer.mozilla.org/en-US/docs/Web/API/URL/URL**/let requestUrl = new URL('https://username:password@developer.mozilla.org:8080/en-US/docs/search.html?par1=abc&par2=123&par3=true#Recent');
let urlParts = {origin: requestUrl.origin,href: requestUrl.href,protocol: requestUrl.protocol,username: requestUrl.username,password: requestUrl.password,host: requestUrl.host,hostname: requestUrl.hostname,port: requestUrl.port,pathname: requestUrl.pathname,search: requestUrl.search,searchParams: {par1: String(requestUrl.searchParams.get('par1')),par2: Number(requestUrl.searchParams.get('par2')),par3: Boolean(requestUrl.searchParams.get('par3')),},hash: requestUrl.hash};
console.log(urlParts);