Regular expression of the syntax is not much to say, you can search their own information to learn. The example in this article is seen in the "Essence of JavaScript language", after reading the regular expression has a further understanding, so share.
Example
This is a regular expression that matches the URL, and the group gets the information for the different parts
var parse_url =/^ (?:( [a-za-z]+):)? (\/{0,3}) ([0-9.\-a-za-z]+) (?::(\d+))? (?:\ /([^?#]*))? (?:\? ([^#]*))? (?:#(.*))?$/;
var url = "Http://www.ora.com:80/goodparts?q#fragment";
var result = parse_url.exec (URL);
var names = ["url", "scheme", "Slash", "host", "Port", "path", "Query", "hash"];var i;
for (i = 0; i < names.length i++) {
Document.writeln (Names[i] + ":" + result[i] + "<br/>");
The output of this code is as follows:
url:http://www.ora.com:80/goodparts?q#fragment
scheme:http
Slash://
Host: www.ora.com
port:80
path:goodparts
query:q
hash:fragment
Analytical
Let's break down the various parts of Parse_url to see how it works:
^
The ^ character represents the beginning of this string, and it is an anchor that directs exec not to skip prefixes that are not like URLs, matching only those strings that start as URLs.
(?:( [a-za-z]+):)?
This factor matches a protocol name, but only matches if it follows a: (colon) followed by it. (?: . . .) Represents a non-capture grouping (noncapturing group). Suffix? Indicates that this grouping is optional, which means that it repeats 0 or 1 times. ( . . .) Represents a captured grouping. A captured grouping copies the text that it matches and places it in the result array. Each captured grouping is assigned a number. The number of the first captured group is 1, so a copy of the text that matches the grouping appears in Result[1]. [ . . .] Represents a character class. A-za-z this character class contains 26 uppercase letters and 26 lowercase letters. Connection character-representation range from A-Z. Suffix + indicates that the character class will be matched one or more times. This group is followed by characters: it is literally matched.
(\/{0,3})
This factor is captured group 2, matched//. \/indicates that the match/(slash) should be. It is escaped with the (backslash) so that it is not incorrectly interpreted as the end of the regular expression. The suffix {0,3} represents/Will match 0~3 times.
([0-9.\-a-za-z]+)
This factor is a capture grouping of 3. It matches a host name, one or more digits, letters, and. Or-character composition. -will be escaped to \-to prevent confusion with the story of the presentation scope.
(?::(\d+))?
This optional factor matches the port number, which is a sequence that consists of one or more digits. \d represents a numeric character. A string of one or more digits is captured by a captured grouping of 4.
(?:\ /([^?#]*))?
This factor is also an optional grouping, matching the path. The group starts with a/. After the character class [^?#] begins with a ^, it means this class contains the addition? and # all characters outside. * indicates that the character class will be matched 0 or more times.
note that my handling here is not rigorous. This class matches the exception? and # All characters, including line Terminator, control character, and many other characters that should not be matched here. In most cases, it will do what we expect, but some malicious text may have a risk of leaking in. An imprecise regular expression is a common source of security vulnerabilities. It is much easier to write a regular expression that is not rigorous than to write a rigorous regular representation.
(?:\? ([^#]*))?
This factor is one with one? The optional grouping to begin with. It contains a capture grouping of 6, which contains 0 or more non-# characters.
(?:#(.*))?
This factor is an optional grouping starting with # ... Matches all characters except the row terminator.
$
$ indicates the end of this string. It guarantees no more content at the end of the URL.
Through this simple example, I believe we have a further understanding of the regular expression, I wish you all a happy study!