I recently discovered a pitfall in regular expression matching in JS, and it was so strange at the time that I even suspected that something paranormal was going on. The following is the pit code var str=["二七1","二七2","金水","二七3","二七4","二七5"] var reg = new RegExp ("二七", "g"); for(var i=0;i<str.length;i++){ if(reg.test(str[i])){ console.log(str[i]) } } I use regular expression to globally match str, and print it out when it is satisfied, so I get this There are two missing for no apparent reason, and I will make a separate judgment on them. var str=["二七1","二七2","金水","二七3","二七4","二七5"] var reg = new RegExp ("二七", "g"); for(var i=0;i<str.length;i++){ if(reg.test(str[i])){ console.log(str[i]) } if(i==1){ console.log(reg.test(str[i])) } if(i==4){ console.log(reg.test(str[i])) } } So I got this One more is missing, but I can see that the two missing ones satisfy the regularity check. Then I found the following passage on the Internet: If a string is successfully matched in a regular expression, lastIndex will be set to the position of the first matched string as the starting point for the next search of the string global match. If subsequent fields can be matched successfully, lastIndex will be repeatedly reassigned until the match fails, and it will be reset to 0. But I asked my teacher, and he told me that after the match is found, lastIndex+1 is returned. That is, when I matched for the first time, lastIndex was 2. This 2 is the subscript in the string, not the subscript of the array. Therefore, when judging str[1], it starts from the string subscript 2, not from 0. Therefore, the second judgment is false, and the match fails. LastIndex is set to 0, so the third match can be successful. So after the judgment result is true, lastIndex is set to 0, so that the data is normal. The data is normal. Summarize If global matching is used, then lastIndex is set to zero after each search, or global matching is not used and direct matching can be performed. Here is a summary of netizens:
Friends in need can refer to it. This is the end of this article about a record of JS regular matching pitfalls. For more relevant JS regular matching pitfalls, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: MySQL knowledge points and commonly used MYSQL commands for the second-level computer exam
>>: Detailed explanation of three ways to cut catalina.out logs in tomcat
In HTML, <, >, &, etc. have special mean...
Now let's summarize several situations of con...
Virtual machines are very convenient testing soft...
1. Connect to MYSQL Format: mysql -h host address...
This article shares the specific code of JavaScri...
Link: https://qydev.weixin.qq.com/wiki/index.php?...
1. Set firewall rules Example 1: Expose port 8080...
Table of contents Overview Object rest attribute ...
Preface The notification bar component is a relat...
User Groups In Linux, every user must belong to a...
What is CN2 line? CN2 stands for China Telecom Ne...
This article example shares the specific code of ...
Table of contents 0x01 Failed to load the driver ...
In daily development, front-end students often ar...
Superset is a lightweight self-service BI framewo...