Regular expressions that match URLs

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.