JavaScript Learning Notes (12) RegExp Type Introduction _ Basics

Source: Internet
Author: User
Tags instance method
1. Match pattern Mark:
The g------represents the global schema, rather than finding that the first match succeeds and ends immediately.
I-------indicates case-insensitive mode
M-----represents multiple-line matches, and the next line continues to match at the end of the line
Copy Code code as follows:

Match first bat or cat, case-insensitive
var pattern1 =/[bc]at/i
var pattern2 = new RegExp ("[Bc]at", "I"); Ditto

Using RegExp is a literal pattern to convert to a string
/\[bc\]at/=======> "\\[bc\\]at"
/\.at/==========> "\\.at"
/name\/age/======> "Name\\/age"
/\d.\d{1,2}/======> "\\d.\\d{1,2}"
/\w\\hello\\123/====> "\\w\\\\hello\\\\123

2.RegExp Instance Properties
global--Boolean value that indicates whether the G flag is set
ignorecase--Boolean value that indicates whether I flag is set
multiline--Boolean value that indicates whether the M flag is set
lastindex--An integer that indicates where to start searching for the next occurrence
source--literal form of string
Copy Code code as follows:

var pattern2 = new RegExp ("\\[bc\\]at", "I");
alert (Pattern2.source); "\[bc\]at"

3.RegExp instance method
The Exec () method accepts a parameter, which is the string that applies the regular expression.
Returns the array and two properties index and input, the first item is a string that matches the entire pattern, and the other is a string that matches the capturing group in the pattern
Copy Code code as follows:

var text "Mom and Dat and baby";
var pattern =/mom (and dad (and baby)?)? /gi;

var matches = pattern.exec (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"

When not in global mode, each call to EXEC () returns the first match
ECEC () returns the next occurrence of the string each time the global mode is called, the value of the lastindex is increased

The test () method takes a parameter, that is, the string that applies the regular expression, and the match returns TRUE or False
Copy Code code as follows:

var text = "000-00-0000";
var pattern =/\d{3}-\d{2}-\d{4}/;

if (pattern.test (test)) {
Alert ("Match!") ");
}

4.RegExp Constructor Properties
Copy Code code as follows:

var text = "This has been a short summer";
var pattern =/(.) host/g;

if (pattern.test (text)) {
alert (regexp.input); This is has been a short summer the last string to match, equivalent to Regexp.$_, opera does not support
alert (Regexp.lastmatch); Short most recent match, equivalent to regexp["$&"], opera does not support
alert (Regexp.lastparen); s most recent match capturing group, equivalent to regexp["$+"], opera does not support
alert (Regexp.leftcontext); This has the text before been a matches, equivalent to regexp["$"]
alert (Regexp.rightcontext); Summer the text after the match, equivalent to regexp["$"],
alert (regexp.multiline); False is not a multiline mode, equivalent to regexp["$*"], opera and IE do not support
}

Regexp.$1 ... Regexp.$9 Storage Capture Group
Copy Code code 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.