Write the following output content in turn.
var reg1 = /a/; var reg2 = /a/g; (('abcabc')); // true (('abcabc')); // true (('abcabc')); // true (('abcabc')); // true (('abcabc')); // true (('abcabc')); // true (('abcabc')); // false (('abcabc')); // true
A very simple regular expression test to find out whether there is a character a in the string abcabc. But the result is a special false, Why?
lastIndex (for regular expressions with parameter g)
In each instantiated RegExp object, there is a lastIndex property with an initial value of 0.
/a/.lastIndex // 0 new RegExp('a').lastIndex // 0 lastIndexIndicates when the match is successful,Match the position of the last character in the original string + 1,That is, the next character matching the contentindex(If the match is at the end of the string,Also return to the position in the original string + 1,That is, stringlength)。If no parameters are includedg,lastIndexAlways0。 var reg = /ab/g; ('123abc'); () // 5 // Match content at the endvar reg = /ab/g; ('123ab'); () // 5 // without parameters gvar reg = /ab/; ('123abc'); () // 0
This lastIndex is the starting position of the match when other matching operations are performed using this rule. When the match fails, reset lastIndex to 0.
var reg = /ab/g; // The initial value is 0, the match is successful from the beginning, the lastIndex is 4(('12ab34ab'), ); // true 4 // Start from the 4th character "3" match. The matching content is the second ab lastIndex is 8.(('12ab34ab'), ); // true 8 // Start match from bit 8 (character length is 8, no bit 8) Match unsuccessful Reset lastIndex to 0(('12ab34ab'), ); // false 0 //Result match in the first step(('12ab34ab'), ); // true 4
After seeing this, the answer will be completed, and the next step is to expand.
For unredeclared regs where errors are prone to.
// Test whether both strings str1 and str2 contain ab charactersvar reg = /ab/g; var str1 = '123ab'; var str2 = 'ab123'; ((str1)); // true ((str2)); // false
It is obvious that the judgment error is caused by lastIndex. Here you can modify reg without parameter g or redeclare reg. Of course, you can also manually modify = 0 after the first match.
Pre-check
Let’s talk about pre-search, which literally means preparing a matching query, that is, querying the following content of the matching content, but only preparing a query to match and does not return.
Often we need to match certain characters in the string followed by certain characters, but we do not need to match the characters followed by the result, for example:
Find all characters following 2 in the string below.
var str = 'a1b2c22d31e4fg6h2'; 'a1b2c22d31e4fg6h2'.match(/[a-z]2/g); // ["b2", "c2", "h2"]
In this way, although the string with 2 can be matched, we do not need the number 2, only characters are needed here. And use pre-check:
'a1b2c22d31e4fg6h2'.match(/[a-z](?=2)/g); // ["b", "c", "h"]
You can see that the conditions are completely met, but how much does the pre-examination cost to the topic of lastIndex in this article?
Let’s use test to see why you use test. Here we need to explain that match matches all, until the match is unsuccessful, and when the match is unsuccessful, the lastIndex is reset to 0.
Exec and test return when the first match is successful or the match fails, and will not continue to match.
var reg1 = /[a-z](?=2)/g; var reg2 = /[a-z]2/g; var str = 'a1b2c22d31e4fg6h2'; ((str), ); // true 3 ((str), ); // true 5 ((str), ); // true 16 ((str), ); // false 0 ((str), ); // true 4 ((str), ); // true 6 ((str), ); // true 17 ((str), ); // false 0
Have you seen the problem? The pre-checked lastIndex does not contain pre-checked content! This can be used to simplify a lot of judgments.
For example, to match the password, we must have at least one uppercase letter, one lowercase letter, one number, and at least 6 digits in length and can only be a combination of numeric letters.
This will be judged according to the situation where you will not be pre-examined:
/[a-z]/.test(pwd) && /[A-Z]/.test(pwd) && /\d/.test(pwd) && /^[a-zA-Z0-9]{6,}$/.test(pwd);
but:
/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z0-9]{6,}$/.test(pwd)
Decompose it and see:
(?=.*[a-z]) Is there a lowercase letter, but it is a pre-check. The match failed and returns false. The lastIndex does not change or is it 0. Similarly, the content of the two out-of-check is understood. Finally, the alphanumeric combination matches with 6 feeds or more. However, the previous pre-checks are all pre-checks, and the lastIndex is never 0. Each match is matched from the beginning, so the requirements are met.
The above is a brief analysis of the lastIndex and pre-examination in regular expressions introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!