A summary of the regular complex patterns in JS elevation

Source: Internet
Author: User

Complex mode:

Group

var redogdog=/dogdog/g;---------------var redogdog=/(dog) {2}/g;

* Citation: (note with parentheses and without parentheses)

var smatch= "#123456789";
var renum=/(# (\d+))/;
Renum.test (Smatch);
Alert (regexp.$1+ "" +regexp.$2);

var schange= "1234 5678";
var rematch=/(\d{4}) (\d{4})/; //Pay attention to spaces in the middle
var newstr=schange.replace (rematch, "$ $"); //not "regexp.$2 regexp.$1"

Alert (regexp.$1+ "" +regexp.$2);//Note this line is output: 1234 5678, there is a sense of the above to cover the regular (deep mechanism is unclear)
Alert (schange+ "" +newstr);

* Candidate: Pipe break |

var s1= "Red";
var s2= "Blue";
var res1s2=/(red|blue)/;
Alert (Res1s2.test (S1));
Alert (res1s2.test (S2));

* Non-capturing grouping

var stomatch= "#123456789";

var renumbers=/# (?: \ d+)/;

Renumbers.test (Stomatch);

alert (regexp.$1);//Output "" Because the group is non-capturing

* Prospective

var stomatch1= "bedroom";
var stomatch2= "bedding";
var rebed=/(bed (? =room))/;
Alert (Rebed.test (STOMATCH1));
alert (regexp.$1);
Alert (Rebed.test (STOMATCH2));

Result: True bed false

* Boundary: ^ Line beginning $ line end \b Word boundary \b non-word boundary

var stomatch= "Important Word is the last one.";
var relastword=/(\w+). $/;
Relastword.test (Stomatch);
alert (regexp.$1);

* Multiline Mode (The following example feels a problem, minus G, the result is not what I think)

var stomatch= "First Second\nthird fourth\nfifth sixth";
var relastwordonline=/(\w+) $/g;
var arrwords=stomatch.match (relastwordonline);
alert (arrWords);

A summary of the regular complex patterns in JS elevation

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.