Positive pre-check and negative pre-verification in regular expressions _ regular expressions

Source: Internet
Author: User

In this paper, the positive and negative pre-investigation in regular expressions is described. Share to everyone for your reference. The specific analysis is as follows:

Let's take a look at an example and then explain what a forward check is.

Example: There is a section of "Coming soon,going GoGoGo" that asks to find COM and go. The code is as follows:

Copy Code code as follows:
<script type= "Text/javascript" >
Forward Advance Check
function T1 () {
var con = document.getelementsbyname (' content ') [0].value;
The var reg =/\b[\w]+ (? =ing\b)/g;//matches the word with ing, but not ing. Note: If ing does not add \b, similar to GOINGABC will also match.
Alert (Con.match (reg));
}
</script>
<body>
<textarea rows= "5" cols= "a" name= "content" ></textarea><br/>
<button onclick= "T1 ();" > Regular pre-check </button><br/>
</body>

In the above example to explain forward check: first detect the boundary, and then find a match with [\w]+ Word, (? =ing) that the first probe back to see if there is ing. If so, match the previous one, and if not, move the cursor backward and continue probing. This process is forward-looking: prejudge to a value.

The so-called negative forward check, is to prejudge not for a certain value. text box input is "win7,winxp,win95,win98,win2003"

Copy Code code as follows:
<script type= "Text/javascript" >
Forward Advance Check
function T1 () {
var con = document.getelementsbyname (' content ') [0].value;
var reg =/\bwin (?! 95\b)/g;//
Alert (Con.match (reg));
}
</script>
<body>
<textarea rows= "5" cols= "a" name= "content" ></textarea><br/>
<button onclick= "T1 ();" > Regular pre-check </button><br/>
</body>

The above two examples are from the forward/negative pre-check----related information Baidu Search "0 wide assertion." JS does not support forward positive/negative pre-check, and PHP support.

I hope this article will help you with your regular expression learning.

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.