Javascript study note (12) RegExp type Introduction

Source: Internet
Author: User

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.
Copy codeThe Code is as follows:
// Match the first bat or cat, case insensitive
Var pattern1 =/[bc] at/I
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
Copy codeThe Code is as follows:
Var pattern2 = new RegExp ("\ [bc \] at", "I ");
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.
Copy codeThe Code is as follows:
Var text "mom and dat and baby ";
Var pattern =/mom (and dad (and baby )?)? /Gi;

Var matches = pattern.exe c (text );
Alert (matches. index); // 0
Alert (matches. input); // "mom and dad and baby"
Alert (matches [0]); // "mom and dad and baby"
Alert (matches [1]); // "and dad and baby"
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.
Copy codeThe Code is as follows:
Var text = "000-00-0000 ";
Var pattern =/\ d {3}-\ d {2}-\ d {4 }/;

If (pattern. test (test )){
Alert ("match! ");
}

4. RegExp constructor attributes
Copy codeThe Code is as follows:
Var text = "this has been a short summer ";
Var pattern =/(.) host/g;

If (pattern. test (text )){
Alert (RegExp. input); // this has been a short summer string to be matched last time, equivalent to RegExp. $ _, not supported by opera
Alert (RegExp. lastMatch); // The last short match, which is equivalent to RegExp ["$ &"]. opera does not support
Alert (RegExp. lastParen); // The last matching capture group in the s, which is equivalent to RegExp ["$ +"]. opera does not support
Alert (RegExp. leftContext); // this has text before been a match, equivalent to RegExp ["$ '"]
Alert (RegExp. rightContext); // the text after the summer match, equivalent to RegExp ["$ '"],
Alert (RegExp. multiline); // false is not a multiline mode. It is equivalent to RegExp ["$ *"] and is not supported by opera or IE.
}

RegExp. $1 ...... RegExp. $9 storage capture group
Copy codeThe Code is as follows:
Var text = "this has been a short summer ";
Var pattern =/(..) or (.)/g;
If (pattern. test (text )){
Alert (RegExp. $1); // sh
Alert (RegExp. $2); // t
}

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.