look out! A "pit" matched by regular test()

Nuggets Anthony 2022-06-23 18:24:16 阅读数:578

lookpitmatchedregulartest

Keep creating , Accelerate growth ! This is my participation 「 Nuggets day new plan · 6 Yuegengwen challenge 」 Of the 24 God , Click to see the event details


Ben Gua believes that you must often use the following simplest regular rules to determine whether there is a certain sub character in a string ( Don't mention it. , I am )

const pattern = /ab/g
pattern.test("abcd") // true

To match like this , Is there anything wrong with that? ?

Don't worry , Now let me give you a guess , The output of the following code ?

const pattern = /ab/g
console.log(pattern.test("abcd"))
console.log(pattern.test("abcd"))
console.log(pattern.test("abcd"))

“ No, just three true Do you ?!”

Try printing on the console ? Isn't surprise 、 No surprise ?

image.png

Why true 、false 、true ?

The original here , There is a small pit here that needs attention , use test() When matching continuously , Will make mistakes , It's because of a person we're going to know —— Regular types lastIndex attribute !

lastIndex Property is used to specify the starting position of the next match .

Every time we use regular RegExp.exec() and RegExp.test() When matching , If returned to true,lastIndex The value of the attribute changes , Will become the last position of the correctly matched substring , And take this position as the starting point for the next retrieval . If returned to false,lastIndex Reset to 0 ;

therefore , Let's try printing like this :

const pattern = /ab/g
console.log(pattern.test("abcd")) // true
console.log(pattern.lastIndex) // 2
console.log(pattern.test("abcd")) // false
console.log(pattern.lastIndex) // 0
console.log(pattern.test("abcd")) // true
console.log(pattern.lastIndex) // 2

When we first call pattern.test("abcd") ,pattern.lastIndex by 2, I.e. letters b The location of , When called again pattern.test("abcd") Will follow b Search back for the location of , Can't find it ab 了 , return false , meanwhile lastIndex Reset to 0 , Then call... For the third time pattern.test("abcd") It can return correctly true 了 ,lastIndex Also became 2.

I don't know this pit , Have you stepped on it ?

image.png

How to solve it ?

Method 1 : Manual cleaning lastIndex

const pattern = /ab/g
console.log(pattern.test("abcd")) // true
pattern.lastIndex = 0
console.log(pattern.test("abcd")) // true

however , This manual cleaning is troublesome , It's stupid , So it is suggested to use method 2 .

Method 2 : use match To match

const pattern = /ab/g
console.log("abcd".match(pattern)) // ['ab']
console.log("abcdab".match(pattern)) // ['ab', 'ab']
console.log("123".match(pattern)) // null

match Is the better solution of the match , No manual cleaning , If it exists, it will be returned as an array , non-existent , return null

image.png


OK, The above is the sharing of this article . Like, follow the comments , Help good writing

I'm Anthony nuggets 100 Popular front-end technology bloggers with a reading volume of 10000 INFP Writing personality persistence 1000 Japanese Geng Wen Pay attention to me , Spend the long programming years with you

版权声明:本文为[Nuggets Anthony]所创,转载请带上原文链接,感谢。 https://qdmana.com/2022/174/202206231720391517.html