javascript uses regular test of the first time is true the second time is false

  • 2021-07-22 08:55:47
  • OfStack

1. Preface

Today, my friend asked me a question. I need to match the same content many times now, but why did I match true directly for the first time and false for the second time?


var s1 = "MRLP";
var s2 = "MRLP";
var reg = /mrlp/ig;
console.log(reg.test(s1));
console.log(reg.test(s2));

At this point, you will find that when we use one regular to match other strings in succession, the first match is true, and the second match is false.

Wait, WHT? I matched MRLP, and I specially added i for case insensitivity, so why can it match normally the first time, but not the second time?

That's what I want to tell you today about lastIndex in JS.

2. lastIndex

Before we begin, let's briefly review the use of regular expressions in JS.

There are two ways to use regular expressions in JS:

The first is the method of regular expression object. There are two common methods.

exec (str): Retrieves the value specified in the string. Returns the value found and determines its location test (str): Retrieves the value specified in the string. Returns true or false

The second is the string object method, and there are four commonly used methods.

match (regexp): Found a match for one or more regular expressions replace (regexp): Replaces a substring that matches a regular expression search (regexp): Retrieves the value that matches the regular expression split (search): Splitting a string into an array of strings

And what do these methods have to do with lastIndex we are talking about today?

The lastIndex attribute specifies the starting position of the next match.

The result of the last match was found by the methods RegExp. exec () and RegExp. test (), both of which start with the location indicated by the lastIndex attribute for the next retrieval.

This allows you to iterate through all the matching text in a string by calling both methods repeatedly.

Also, it should be noted that this property can only be used by setting the flag g.

Now that we know the reason for the formation of this thing, it is very simple to solve it.

3. Solutions

3.1 Solution 1

As mentioned above, our lastIndex attribute must be set with the g tag before it can be used.

Then when we match, we can directly remove the g tag according to the situation.


var s1 = "MRLP";
var s2 = "MRLP";
var reg = /mrlp/i;
console.log(reg.test(s1)); //true
console.log(reg.test(s2)); //true

3.2 Solution 2

Most of the time, we have to perform a global match (g), so we can't use the first scheme at this time.

In fact, our lastIndex attribute is readable and writable.

It can be set as soon as the next search for the target string begins.

When methods exec () or test () can no longer find matching text, they automatically reset the lastIndex property to 0.

In this way, when we perform the global match again, the false situation will not occur.


var s1 = "3206064928 : MRLP : 3206064928";
var s2 = "MRLP";
var reg = /mrlp/ig;
console.log(reg.test(s1)); //true
console.log(reg.lastIndex);  //reg.lastIndex = 15
reg.lastIndex = 0;     // Here I will  lastIndex  Reset to  0
console.log(reg.test(s2)); //true

Thank you for reading, hope to help everyone, thank you for your support to this site!


Related articles: