Definitions and usage examples of forward and backward looking regular expressions in JS

Source: Internet
Author: User
This article mainly introduces the definition and usage of JS forward and backward regular expressions, and analyzes the specific definitions, functions, and usage skills of forward and backward regular expressions, for more information, see the examples in this article to describe the definition and usage of forward and backward looking regular expressions in JS. We will share this with you for your reference. The details are as follows:

Definition

X (? = Y) Match 'X' only when 'X' is followed by 'y'. This is called forward lookup.

For example,/Jack (? = Sprat)/matches 'jack' only when it is followed by 'sprat '. /Jack (? = Sprat | Frost)/matches 'jack' only when it is followed by 'sprat' or 'frost '. However, neither 'sprat' nor 'frost' is part of the matching result.

X (?! Y) Match 'X' only when 'X' is not followed by 'y'. This is called forward negative lookup.

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

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

Example:

            Script var testStr = "windows 95"/* 1-no matching expression */var testReg =/^ windows. * $/var result = testStr. match (testReg); console. log ("/^ windows. * $/= "+ result) // ^ windows. * $/= windows 95/* 2-expression matching with the tape */var testReg =/^ windows (. *) $/var result = testStr. match (testReg); console. log ("/^ windows (. *) $/= "+ result) // ^ windows (. *) $/= windows/* 3-the matching result is not recorded. */var testReg =/^ windows (? :. *) $/Var result = testStr. match (testReg); console. log ("/^ windows (? :. *) $/= "+ Result) // ^ windows (? :. *) $/= Windows 95/* 4-forward matching, matching location, positive matching */var testReg =/^ windows (? = 95) 95 $/var result = testStr. match (testReg); console. log ("/^ windows (? =. *) $/= "+ Result) // ^ windows (? =. *) $/= Windows 95/* 5-forward matching, matching location, negative matching */var testStr = "windows me" var testReg =/^ windows (?! 95) me $/var result = testStr. match (testReg); console. log ("/^ windows (?! \ D *) $/= "+ result) // ^ windows (?! D *) $/= windows me script

I hope this article will help you design JavaScript programs.

For more articles on definitions and examples of Regular Expressions in front and back direction of JS, refer to the PHP Chinese website!

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.