Ambiguity of parentheses in Regular Expressions

Source: Internet
Author: User

Limit the scope of the quantifiers. The range of the multiple choice structure is reverse reference. Capture text groups. capture only groups. do not capture foresight. 1. define the range of quantifiers. var reg1 =/(Matz )? // 0 or one Matzvar reg2 =/(Matz) +/; // Matzvar reg3 =/(Matz )*/; // 0 or multiple Matz 2. Specify the range of multiple-choice structures var reg =/(Matz | Eich)/reg. test ('matz') // => truereg. test ('eich ') // => truereg. test ('john') // => false 3. Capture the text var reg =/(boy) \ 1 // equivalent to/boyboy/reg for reverse reference. test ('boys') // => falsereg. test ('boyboy ') // => true var reg/(boy) (girl) \ 1 \ 2/reg. test ('boygirlboygir') // => true 4. Group capture var reg1 =/(\ d {3}) (\ d {3 })/ Var str = '2014 111 'str. replace (reg1, '$2 $ 1') // => '2017 111111'. Pay attention to the $2, $1, stores the matched string var reg2 =/(\ d {3}) (\ d {4}) (\ d {4})/var mobile = '000000' reg2. test (mobile) RegExp. $1 // => 135RegExp. $2 // => 2272RegExp. $3 // => 2724 var reg3 =/(\ d {3}) (\ d {4}) (\ d {4 }) /var mobile = '000000' mobile. replace (reg3, '$1 $2 $ 3') // => '2017 135 2272 1111' 5. Only groups do not capture (and "?: "Together) var reg = /(?: \ D +)/reg. test ('20140901') RegExp. $1 // => ''in a regular expression that does not store long matching elements, reverse referencing reduces the matching speed and performance. grouping is not captured when reverse referencing is not required. 6. lookahead and "? = ") It tells the regular expression to look forward to some characters but not move the position. Forward does not match any character and only matches the specific position in the text. Var reg =/(John )(? = Resig)/reg. test ('john') // => falsereg. test ('John backus') // => falsereg. test ('John reisg') // => trueRegExp. $1 // => 'john'. Note that this is not "John Resig"

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.