当前位置:网站首页>On the bug of JS regular test method

On the bug of JS regular test method

2022-04-23 13:24:00 mb625d3d7cd8cc1

Actually, I seldom use this , So I didn't pay attention to this problem before , I went to see this thing only after defoliation wrote a abnormal test

Actually, I seldom use this , So I didn't pay attention to this problem before , I went to see this thing only after defoliation wrote a abnormal test

The following code is all in chrome Of F12 Next debugging , You can study

Let's start with something .

      
      
var re = /\d/;
console.log( re.test("1") );
console.log( re.test("1") );
console.log( re.test("1") );
console.log( re.test("1") );
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

All is true That's all right. ..

 Talking about js Regular test Method bug piece _ character string

But you put /\d/; Change to /\d/g; Try again. .

 Talking about js Regular test Method bug piece _ Regular _02


Revise again :


      
      
console.log( /\d/g.test("1") );
console.log( /\d/g.test("1") );
console.log( /\d/g.test("1") );
console.log( /\d/g.test("1") );
  • 1.
  • 2.
  • 3.
  • 4.

 Talking about js Regular test Method bug piece _chrome_03

All is true, Why on earth is this ?

These results are quite interesting , Of course, experts naturally know why , If you know , In fact, you can skip the following without looking , It's all hydrology ..

There is one in the regular  ​​lastIndex​​  Properties of , It's the start of the next match .

      
      
var re = /\d/g;
console.log( re.test("1"), re.lastIndex );
console.log( re.test("1"), re.lastIndex );
console.log( re.test("1"), re.lastIndex );
console.log( re.test("1"), re.lastIndex );
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

 Talking about js Regular test Method bug piece _ character string _04

You can see The first matching result is  true  Indicates that the match is successful , here  lastIndex  Record the starting position of the next match as 1.

So the second match from "1" String index 1 Location matching for , Of course, the match failed , Because this string has only one character , His index is 0.

and  /\d/g.test("1")  This is why every successful match can ?

Because it directly uses regular literals , It's equivalent to recreating one regular object at a time ,lastIndex  The initial value of the property is 0.

So you can match successfully every time .

Now do you understand , Include  exec  It's the same thing , Match one at a time ,lastIndex  Record the starting position of the next match .

If you have to use a regular object , Then only every time  test  Pre reset  lastIndex  了 , So we can make sure he doesn't have an accident .

      
      
var re = /\d/g;
console.log( re.test("1") );
re.lastIndex = 0;
console.log( re.test("1") );
re.lastIndex = 0;
console.log( re.test("1") );
re.lastIndex = 0;
console.log( re.test("1") );
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

 Talking about js Regular test Method bug piece _chrome_05

Okay , It took a lot of time to modify the syntax highlighting plug-in today , So water wrote an article , I hope you will have a better understanding .

版权声明
本文为[mb625d3d7cd8cc1]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231151524455.html