Javascript Study Notes (12) RegExp type

Source: Internet
Author: User

Previous: http://www.bkjia.com/kf/201206/134143.html


1. Matching mode flag:
G ------ indicates the global mode, instead of immediately ending when the first match is found to be successful.
I ------- indicates the case-insensitive mode.
M ----- indicates multi-row matching. The matching continues for the next row at the end of a row.
1 // match the first bat or cat, case insensitive
2 var pattern1 =/[bc] at/I
3 var pattern2 = new RegExp ("[bc] at", "I"); // same as above
Using RegEXp is the literal mode to convert to a string
/\ [Bc \] at/======> "\ [bc \]"
/\. At/==========> "\."
/Name \/age/======> "name \/age"
/\ D. \ d {1, 2}/=> "\ d. \ d {1, 2 }"
/\ W \ hello \ 123/=> "\ w \ hello \ 123
2. RegExp instance attributes
Global -- Boolean value, indicating whether the g flag is set
IgnoreCase -- Boolean value, indicating whether the I flag is set
Multiline -- Boolean value, indicating whether the m flag is set
LastIndex -- integer, indicating the position of the next match
Source -- literal string
1 var pattern2 = new RegExp ("\ [bc \] at", "I ");
2 alert (pattern2.source); // "\ [bc \]"
3. RegExp instance method
The exec () method accepts a parameter, that is, a string that uses a regular expression,
Returns the array and two attributes index and input. The first entry of the array is the string that matches the entire pattern, and the other items are the strings that match the capture group in the pattern.
 
1 var text "mom and dat and baby ";
2 var pattern =/mom (and dad (and baby )?)? /Gi;
3
4 var matches = pattern.exe c (text );
5 alert (matches. index); // 0
6 alert (matches. input); // "mom and dad and baby"
7 alert (matches [0]); // "mom and dad and baby"
8 alert (matches [1]); // "and dad and baby"
9 alert (matches [2]); // "and baby"
 
In non-Global mode, the first matching item is returned every time exec () is called.
In global mode, each time ecec () is called to return the next matching item of the string, the value of lastIndex will increase.

The test () method accepts a parameter, that is, a string that uses a regular expression. true is returned for matching. Otherwise, false is returned.
 
1 var text = "000-00-0000 ";
2 var pattern =/\ d {3}-\ d {2}-\ d {4 }/;
3
4 if (pattern. test (test )){
5 alert ("match! ");
6}
 
4. RegExp constructor attributes
 
1 var text = "this has been a short summer ";
2 var pattern =/(.) host/g;
3
4 if (pattern. test (text )){
5 alert (RegExp. input); // this has been a short summer string to be matched last time, equivalent to RegExp. $ _, not supported by opera
6 alert (RegExp. lastMatch); // The last short match, which is equivalent to RegExp ["$ &"]. opera does not support
7 alert (RegExp. lastParen); // capture group of the last match, equivalent to RegExp ["$ +"], not supported by opera
8 alert (RegExp. leftContext); // The text before this has been a match is equivalent to RegExp ["$ '"]
9 alert (RegExp. rightContext); // the text after the summer match, equivalent to RegExp ["$ '"],
10 alert (RegExp. multiline); // false is not a multiline mode. It is equivalent to RegExp ["$ *"] and is not supported by opera or IE.
11}
 
RegExp. $1 ...... RegExp. $9 storage capture group
 
1 var text = "this has been a short summer ";
2 var pattern =/(..) or (.)/g;
3 if (pattern. test (text )){
4 alert (RegExp. $1); // sh
5 alert (RegExp. $2); // t
6}
 

 


From Sunday walk

Related Article

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.