Case analysis of positive and negative prefetch in regular expressions

Time:2020-5-19

In this paper, the positive and negative pre queries in regular expressions are illustrated. Share with you for your reference. The specific analysis is as follows:

Here’s an example to explain what a forward lookup is.

Example: there is a paragraph called “coming soon, going go gogogo”, which requires finding out com and go. The code is as follows:

Copy codeThe code is as follows:
<html>
<head>
<script type=”text/javascript”>
//Forward look up
function t1(){
 var con = document.getElementsByName(‘content’)[0].value;
Var reg = / \ B [\ w] + (? = ing \ b) / g; / / matches words with ing, but not ing. Note: if you do not add B after ing, similar to goingabc will match.
 alert(con.match(reg));
}
</script>
</head>
<body>
<textarea rows=”5″ cols=”30″ name=”content”></textarea><br />
< button onclick = “t1();” > regular pre query < / button > < br / >
</body>
</html>

Use the above example to explain the forward pre query: first find out the boundary, and then find the word matching [\ w] +, (? = ing) means to detect backward first to see if there is ing. If yes, match the previous ones; if no, move the cursor one bit back to continue the detection. This process is called forward prefetch: to determine a value in advance.

The so-called negative pre check means that the pre judgment is not a certain value. The input content of text box is “win7, WINXP, Win95, Win98, Win2003”

Copy codeThe code is as follows:
<html>
<head>
<script type=”text/javascript”>
//Forward look up
function t1(){
 var con = document.getElementsByName(‘content’)[0].value;
 var reg = /\bwin(?!95\b)/g;//
 alert(con.match(reg));
}
</script>
</head>
<body>
<textarea rows=”5″ cols=”30″ name=”content”></textarea><br />
< button onclick = “t1();” > regular pre query < / button > < br / >
</body>
</html>

The above two examples are forward / negative pre query from the front to the back – related information Baidu search “zero width assertion”. JS does not support forward / negative lookups, while PHP does.

I hope that this article will be helpful for your regular expression learning.

Recommended Today

Webpage WB.ExecWB Control printing method call description and parameter introduction

Webpage WB.ExecWB Control printing method Copy code The code is as follows: <table width=”100%” cellpadding=”1″ onmouseover=”kpr.style.display=”;” > <tr> <td width=”100%” height=”25″ colspan=”3″><script language=”javascript”> function printsetup(){ //Print page setupwb.execwb(8,1); } function printpreview(){ //Print page previewwb.execwb(7,1); } function printit() { Are you sure you want to print? ‘)){wb.execwb(6,6) } } </script> <OBJECT classid=”CLSID:8856F961-340A-11D0-A96B-00C04FD705A2″ height=0 id=wb name=wb width=3></OBJECT> […]