Forward and backward regex expressions and their JS examples

Source: Internet
Author: User

definition

x(?=y)Match ' x ' only when ' x ' follows ' Y '. This is called positive lookup .

For example,/jack (? =sprat)/Will match to ' Jack ' only when it is followed by ' Sprat '. /jack (? =sprat| Frost)/Match ' Jack ' only when it is followed by ' sprat ' or ' Frost '. But ' sprat ' and ' Frost ' are not part of the matching results.

x(?!y)Match ' x ' only when ' X ' is not followed by ' Y ', this is called positive negation lookup.

For example,/\d+ (?! \.) /Match a number only when the number is not followed by a decimal point. Regular expression/\d+ (?! \.) /.exec ("3.141") matches ' 141 ' but not ' 3.141 '

Form Https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Guide/Regular_Expressions

See http://www.cnblogs.com/dolphinx/p/3486214.html explain the comparison understood.

Example
<HTML><Head> </Head> <Body>    <inputID= "Test"type= "text"value="" />    <inputID= "Test"type= "text"value="" />    <inputID= "Test"type= "text"value="" />    <inputID= "Test"type= "text"value="" />    <inputID= "Test"type= "text"value="" />    <Script>    varTeststr= "Windows"        /*1-no band expression match*/    varTestreg= /^windows. *$/    varresult=Teststr.match (Testreg); Console.log ("/^windows. *$/="+result)///^windows. *$/=windows        /*2-band expression matching*/    varTestreg= /^windows (. *) $/    varresult=Teststr.match (Testreg); Console.log ("/^windows (. *) $/="+result)///^windows (. *) $/=windows 95,95        /*3-A band expression that does not record its matching results*/    varTestreg= /^windows (?:. *) $/    varresult=Teststr.match (Testreg); Console.log ("/^windows (?:. *) $/="+result)///^windows (?:. *) $/=windows        /*4-forward match, match position, positive match*/    varTestreg= /^windows (? =95) 95$/    varresult=Teststr.match (Testreg); Console.log ("/^windows (? =.*) $/="+result)///^windows (=.*) $/=windows    /*5-forward match, match position, negative match*/    varTeststr= "Windows Me"    varTestreg= /^windows (?! me$)/    varresult=Teststr.match (Testreg); Console.log ("/^windows (?! \d*) $/="+result)///^windows (?! d*) $/=windows me        </Script></Body></HTML>

Forward and backward regex expressions and their JS examples

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.