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!