Detailed explanation of JS Regular Expressions

Source: Internet
Author: User

G indicates global match.
M indicates that multiple rows can be matched.
I indicates case-insensitive matching.
^ Match the start position of the input string
$ Match the end position of the input string
* Match the previous subexpression zero or multiple times. equivalent to {0 ,}
+ Match the previous subexpression once or multiple times. equivalent to {1 ,}
? Matches the previous subexpression zero or one time. It is equivalent to [}. When this character follows any other delimiter (*, + ,?, The matching mode after {n}, {n ,}, {n, m}) is not greedy. The non-Greedy mode matches as few searched strings as possible, while the default greedy mode matches as many searched strings as possible. For example, for strings "oooo", 'O ++? 'Will match a single "O", and 'O +' will match all 'O '.

\ D matches a numeric character. It is equivalent to [0-9]
\ D matches a non-digit character. It is equivalent to [^ 0-9]
\ W, equivalent to "[A-Za-z0-9 _]"
\ W matches any non-word character, equivalent to "[^ A-Za-z0-9]"
\ S matches any blank characters, including space tabs, and so on. It is equivalent to [\ f \ n \ r \ t \ v]
\ S matches any non-blank characters. It is equivalent to [^ \ f \ r \ n \ t \ v]
\ B matches a word boundary, that is, the position between a word and a space.
\ B matches non-word boundaries.

(Pattern) matches pattern and obtains this match. The obtained match can be obtained from the generated matches set. The submatches set is used in VBScript, and $0… is used in JScript... $9 attribute.

(? : Pattern) matches pattern but does not get the matching result. That is to say, this is a non-get match and is not stored for future use. This is useful when you use the "or" character (|) to combine each part of a pattern. For example, 'industr (? : Y | ies) is a simpler expression than 'industry | industries.

(? = Pattern) Forward pre-query: matches the search string at the beginning of any string that matches pattern. This is a non-get match, that is, the match does not need to be obtained for future use. For example, 'windows (? = 95 | 98 | nt | 2000) 'can match "Windows" in "Windows 2000", but cannot match "Windows" in "Windows 3.1 ". Pre-query does not consume characters, that is, after a match occurs, the next matching search starts immediately after the last match, instead of starting after the pre-query characters.

(?! Pattern) negative pre-query, match the search string at the start of any string that does not match negative lookahead matches the search string at any point where a string not matching pattern. This is a non-get match, that is, the match does not need to be obtained for future use. For example, 'windows (?! 95 | 98 | nt | 2000) 'can match "Windows" in "Windows 3.1", but cannot match "Windows" in "Windows 2000 ". Pre-query does not consume characters. That is to say, after a match occurs, the next matching search starts immediately after the last match, instead of starting after the pre-query characters.

Match 2-4 Chinese Characters

Program Code /^ [\ U4e00-\ u9fa5] {2, 4} $/g;

Matches 6 to 18 characters (letters, numbers, underscores)

Program code/^ \ W {6, 18} $ /;

 

Program code/^ [A-Za-z0-9 _] $ /;

Matching HTML tags

Program code/<[^>] *> | <\/[^>] *>/GM;

 

Program code/<\/? [^>] +>/GM;

Match spaces at both ends

Program code/(^ \ s *) | (\ s * $)/g;

Priority (from high to low)
\ Escape Character
(),(? :),(? =), [] Parentheses and square brackets
*, + ,? , {N}, {n ,}, {n, m} qualifier
^, [Vapour: content] nbsp; location and Sequence
| "Or" Operation

Match two consecutive words separated by Spaces

Program code/\ B ([A-Z] +) \ 1 \ B/Gim;

In this example, the subexpression is each of the parentheses.
The captured expression contains one or more letter characters, that is, specified by '[A-Z] +.
The second part of the regular expression is a reference to the previously captured sub-match, that is, the second occurrence word matched by the append expression.
'\ 1' is used to specify the first child match. The word boundary character ensures that only individual words are detected.
Otherwise, phrases such as "is issued" or "this is" are incorrectly recognized by this expression.

Program code var Ss = "is the cost of gasoline going up ?. Is it the cost of gasoline going up ?. ";
VaR Re =/\ B ([A-Z] +) \ 1 \ B/Gim;
VaR Rv = ss. Replace (Re, "$1 ");
Document. Write (RV) // output "is the cost of gasoline going up ?. Is the cost of gasoline going up ?. "

Program code/\ bcha/

Match the first three characters of the word 'Chapter 'because they appear after the word boundary

Program code/TER \ B/

Match the 'ter 'in the word 'Chapter' because it appears before the word boundary

Program code/\ bapt/

Match 'apt 'because it is located in the middle of 'Chapter', but it does not match 'apt 'in 'aptitude' because it is located after the word boundary
*/

Match URL

Program code/(\ W +): \/([^ \/:] +) (: \ D *)? ([^ #] *)/

Break down the following Uris into the Protocol (FTP, HTTP, etc), domain name address, and page/path:
Http://msdn.microsoft.com: 80/scripting/default.htm

The first additional sub-expression is the protocol part used to capture the web address. This subexpression matches any word that is located before a colon and two forward slashes. The second append subexpression captures the domain name address of the address. This subexpression does not match any character sequence of the '^', '/', or ':' character. The third append subexpression captures the website port number. If this port number is specified. This subexpression matches zero or multiple numbers followed by a colon. Finally, the fourth additional sub-expression captures the path specified by the web address and the \ or page information. This subexpression matches one or more characters except '#' or spaces.

After applying the regular expression to the URI shown above, the Child match contains the following content:

Regexp. $1 contains "HTTP"

Regexp. $2 contains "msdn.microsoft.com"

Regexp. $3 contains ": 80"

Regexp. $4 contains "/scripting/default.htm"

Regular Expression Method

1 Test Method

Returns a Boolean value indicating whether the pattern exists in the searched string.

Rgexp. Test (STR)

The attributes of the global Regexp object cannot be modified by the test method.

Example1

Program code var url =" Http://msdn.microsoft.com: 80/scripting/default.html ";
VaR Reg =/(\ W +): \/([^ \/:] +) (: \ D *)? ([^ #] *)/;
VaR flag = reg. Test (URL );
Flag // return true
Regexp. $1 // return "HTTP"
Regexp. $2 // return "msdn.microsoft.com"
Regexp. $3 // return ": 80"
$ Egexp. $4 // return "/scripting/default.html"


The search and test methods cannot update global Regexp objects. Therefore, Regexp. Input, Regexp. index, Regexp. lastindex return undefined.

2. Match Method

Use the regular expression mode to perform a search on the string and return the result containing the search as an array.

Program code stringobj. Match (rgexp)

If no match is found in the match method, null is returned. If a match is found, an array is returned and the attributes of the global Regexp object are updated to reflect the matching result.

The array returned by the match method has three attributes: input, index, and lastindex.
The INPUT attribute contains the entire string to be searched.
The index attribute contains the position of the matched substring in the entire searched string.
The lastindex attribute contains the next position of the last character in the last match.

If the global sign (G) is not set, the 0 element of the array contains the entire match, and the 1st to nelement contains any child match that has occurred during the match.
This is equivalent to the exec method without a global flag. If a global flag is set, elements 0 to N contain all matches.

Example1 (no global flag is set)

Program code var url =" Http://msdn.microsoft.com: 80/scripting/default.html ";
VaR Reg =/(\ W +): \/([^ \/:] +) (: \ D *)? ([^ #] *)/;
VaR myarray = URL. Match (REG );
Regexp. $1 // return "HTTP"
Regexp. $2 // return "msdn.microsoft.com"
Regexp. $3 // return ": 80"
$ Egexp. $4 // return "/scripting/default.html"
Myarray // return myarray [0] =" Http://msdn.microsoft.com: 80/scripting/default.html ",
Myarray [1] = "HTTP", myarray [2] = "msdn.microsoft.com ",
Myarray [3] = ": 80", myarray [4] = "/scripting/default.html"
Myarray. Input // return" Http://msdn.microsoft.com: 80/scripting/default.html "
Myarray. index // return 0
Myarray. lastindex // Return 51


Example2 (with global flag set)

Program code var url =" Http://msdn.microsoft.com: 80/scripting/default.html ";
VaR Reg =/(\ W +): \/([^ \/:] +) (: \ D *)? ([^ #] *)/G;
VaR myarray = URL. Match (REG );
Regexp. $1 // return "HTTP"
Regexp. $2 // return "msdn.microsoft.com"
Regexp. $3 // return ": 80"
$ Egexp. $4 // return "/scripting/default.html"
Myarray // return myarray =" Http://msdn.microsoft.com: 80/scripting/default.html "
Myarray. Input // return" Http://msdn.microsoft.com: 80/scripting/default.html "
Myarray. index // return 0
Myarray. lastindex // Return 51


Note the difference after the global flag is set
If the global sign (G) is not set, the 0 element of the array contains the entire match, and the 1st to nelement contains any child match that has occurred during the match.
This is equivalent to the exec method without a global flag. If a global flag is set, elements 0 to N contain all matches.

3 exex Method

Run the search in the string in regular expression mode and return an array containing the search result.

Program code rgexp.exe C (STR)

If the exec method does not find a match, it returns NULL.
If it finds a match, the exec method returns an array and updates the attributes of the global Regexp object to reflect the matching result.
The 0 element of the array contains the complete match, and the 1st to nelement contains any child match in the match.
This is equivalent to the match method without setting the global flag.

If a global flag is set for the regular expression, exec searches for it at the position indicated by the value of lastindex.
If the global flag is not set, exec ignores the value of lastindex and searches from the starting position of the string.

The array returned by the exec method has three attributes: input, index, and lastindex.
The INPUT attribute contains the entire searched string.
The index attribute contains the position of the matched substring in the searched string.
The lastindex attribute contains the next position of the last character in the match.

Example1 (no global flag is set)

Program code var url =" Http://msdn.microsoft.com: 80/scripting/default.html ";
VaR Reg =/(\ W +): \/([^ \/:] +) (: \ D *)? ([^ #] *)/;
VaR myarray=reg.exe C (URL );
Regexp. $1 // return "HTTP"
Regexp. $2 // return "msdn.microsoft.com"
Regexp. $3 // return ": 80"
$ Egexp. $4 // return "/scripting/default.html"
Myarray // return myarray [0] =" Http://msdn.microsoft.com: 80/scripting/default.html ",
Myarray [1] = "HTTP", myarray [2] = "msdn.microsoft.com ",
Myarray [3] = ": 80", myarray [4] = "/scripting/default.html"
Myarray. Input // return" Http://msdn.microsoft.com: 80/scripting/default.html "
Myarray. index // return 0
Myarray. lastindex // Return 51


If the global flag is not set, the match method is the same as the exec method.

Example2 (set global flag)

Program code var url =" Http://msdn.microsoft.com: 80/scripting/default.html ";
VaR Reg =/(\ W +): \/([^ \/:] +) (: \ D *)? ([^ #] *)/;
VaR myarray=reg.exe C (URL );
Regexp. $1 // return "HTTP"
Regexp. $2 // return "msdn.microsoft.com"
Regexp. $3 // return ": 80"
$ Egexp. $4 // return "/scripting/default.html"
Myarray // return myarray [0] =" Http://msdn.microsoft.com: 80/scripting/default.html ",
Myarray [1] = "HTTP", myarray [2] = "msdn.microsoft.com ",
Myarray [3] = ": 80", myarray [4] = "/scripting/default.html"
Myarray. Input // return" Http://msdn.microsoft.com: 80/scripting/default.html "
Myarray. index // return 0
Myarray. lastindex // Return 51


4. Search Method

Returns the position of the first substring that matches the regular expression.

Program code stringojb. Search (rgexp)

The search method specifies whether a matching exists.
If a match is found, the search method returns an integer indicating the offset position starting from the string.
If no match is found,-1 is returned.

Example1

Program code var url =" Http://msdn.microsoft.com: 80/scripting/default.html ";
VaR Reg =/(\ W +): \/([^ \/:] +) (: \ D *)? ([^ #] *)/;
VaR flag = URL. Search (REG );
Flag // return 0
Regexp. $1 // return "HTTP"
Regexp. $2 // return "msdn.microsoft.com"
Regexp. $3 // return ": 80"
$ Egexp. $4 // return "/scripting/default.html"


The search and test methods cannot update global Regexp objects. Therefore, Regexp. Input, Regexp. index, Regexp. lastindex return undefined.

5 replace Method

Returns the copy of the string after text replacement based on the regular expression.

Update global Regexp object

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.