Tag: the regular expression script character indicates that the start expression regular escape character is not
See the URL's regular in the JavaScript language essence and record it
var parse_url =/^ (?:( [a-za-z]+]:)? (\/{0,3}) ([0-9.\-a-za-z]+) (?::(\d+))? (?:\ /([^?#]*))? (?:\? ([^#]*))? (?:#(.*))?$/;
var url = "Http://www.ora.com:80/goodparts?q#fragment";
The implementation of the specific code is shown below:
var url = "Http://www.ora.com:80/goodparts?q#fragment";
var parse_url =/^ (?:( [a-za-z]+]:)? (\/{0,3}) ([0-9.\-a-za-z]+) (?::(\d+))? (?:\ /([^?#]*))? (?:\? ([^#]*))? (?:#(.*))?$/;
var result = parse_url.exec (URL);
var names = [' url ', ' scheme ', ' slash ', ' host ', ' Port ', ' path ', ' query ', ' hash '];
var blanks = ';
var i;
for (i = 0; i < names.length; i + = 1) {
Document.writeln (Names[i] + ': ' + blanks.substring (names[i].length), result[i]);
}
Execution Result:
Url:http://www.ora.com:80/goodparts?q#fragment
Scheme:http
Slash://
Host:www.ora.com
Port:80
Path:goodparts
Query:q
Hash:fragment
A partial explanation of the regular part of the above URL:
/^(?:( [a-za-z]+]:)? (\/{0,3}) ([0-9.\-a-za-z]+) (?::(\d+))? (?:\ /([^?#]*))? (?:\? ([^#]*))? (?:#(.*))? $/
1.^ = Start $ = End
/^(?:( [a-za-z]+):)? (\/{0,3}) ([0-9.\-a-za-z]+) (?::(\d+))? (?:\ /([^?#]*))? (?:\? ([^#]*))? (?:#(.*))?$/
2. (?:p Attern) that matches pattern but does not get matching results
(?:( [a-za-z]+]:) is a match ([a-za-z]+):, but returns only the result of the match ([a-za-z]+), and does not return:
/^(?:( [a-za-z]+]:)? (\/{0,3}) ([0-9.\-a-za-z]+) (?::(\d+))? (?:\ /([^?#]*))? (?:\? ([^#]*))? (?:#(.*))?$/
3. \*{3}, because * is a special character, so with an escape character, the expression represents a match of 3 *, that is * * *
The same \/{0,3} indicates a match of 0 to 3/, i.e. no//,///,//
/^(?:( [a-za-z]+]:)? (\/{0,3}) ([0-9.\-a-za-z]+) (?::(\d+))? (?:\ /([^?#]*))? (?:\? ([^#]*))? (?:#(.*))?$/
4. ^ in square brackets means non-meaning
[^?#] means not? and other characters that are not #
/^(?:( [a-za-z]+]:)? (\/{0,3}) ([0-9.\-a-za-z]+) (?::(\d+))? (?:\ /([^?#]*))? (?:\? ([^#]*))? (?:#(.*))?$/
* Match the previous item 0 or more times. That is {0,}
? Matches the previous item 0 or 1 times. That is {0,1}
Regular expressions that match URLs