This time I will bring you a detailed explanation of the use of lastIndex in regular expressions. What are the precautions for using lastIndex in regular expressions? The following is a practical case, let's take a look.
Write the following output content in sequence.var reg1 = /a/; var reg2 = /a/g; console.log(reg1.test('abcabc')); // true console.log(reg1.test('abcabc')); // true console.log(reg1.test('abcabc')); // true console.log(reg1.test('abcabc')); // true console.log(reg2.test('abcabc')); // true console.log(reg2.test('abcabc')); // true console.log(reg2.test('abcabc')); // false console.log(reg2.test('abcabc')); // true
regular expressiontest to find whether there is a character a in the stringabcabc. But there is a special false in the result. Why?
lastIndex (for regular expression with parameter g)
In each instantiatedRegExp object , there is a lastIndex attribute whose initial value is 0.
/a/.lastIndex // 0 new RegExp('a').lastIndex // 0 lastIndex表示匹配成功时候,匹配内容最后一个字符所在原字符串中的位置 + 1,也就是匹配内容的下一个字符的index(如果匹配内容在字符串的结尾,同样返回原字符串中的位置 + 1,也就是字符串的length)。如果未带参数g,lastIndex始终为0。 var reg = /ab/g; reg.test('123abc'); console.log(reg.lastIndex) // 5 // 匹配内容在最后 var reg = /ab/g; reg.test('123ab'); console.log(reg.lastIndex) // 5 // 不带参数g var reg = /ab/; reg.test('123abc'); console.log(reg.lastIndex) // 0
var reg = /ab/g; // 初始值为0,从最开始匹配 匹配成功, lastIndex为4 console.log(reg.test('12ab34ab'), reg.lastIndex); // true 4 // 从第4位字符"3"开始匹配 匹配内容为第二个ab lastIndex 为 8 console.log(reg.test('12ab34ab'), reg.lastIndex); // true 8 // 从第8位 (字符长度为8,没有第8位) 开始匹配 匹配不成功 重置lastIndex 为 0 console.log(reg.test('12ab34ab'), reg.lastIndex); // false 0 // 从头匹配 同第一步 console.log(reg.test('12ab34ab'), reg.lastIndex); // true 4
// 测试字符串str1 和 str2 是否都含有ab字符 var reg = /ab/g; var str1 = '123ab'; var str2 = 'ab123'; console.log(reg.test(str1)); // true console.log(reg.test(str2)); // false
Pre-check
Then let’s talk about pre-check, which literally means to prepare the matching query, that is, to query the next content of the matching content, but only The prepared query matches and does not return. Often we need to match certain characters in a string followed by certain characters, but we do not need to include the following characters in the matching result, for example: Find out the following characters in the string Characters are followed by all characters of 2.var str = 'a1b2c22d31e4fg6h2'; 'a1b2c22d31e4fg6h2'.match(/[a-z]2/g); // ["b2", "c2", "h2"]
'a1b2c22d31e4fg6h2'.match(/[a-z](?=2)/g); // ["b", "c", "h"]
var reg1 = /[a-z](?=2)/g; var reg2 = /[a-z]2/g; var str = 'a1b2c22d31e4fg6h2'; console.log(reg1.test(str), reg1.lastIndex); // true 3 console.log(reg1.test(str), reg1.lastIndex); // true 5 console.log(reg1.test(str), reg1.lastIndex); // true 16 console.log(reg1.test(str), reg1.lastIndex); // false 0 console.log(reg2.test(str), reg2.lastIndex); // true 4 console.log(reg2.test(str), reg2.lastIndex); // true 6 console.log(reg2.test(str), reg2.lastIndex); // true 17 console.log(reg2.test(str), reg2.lastIndex); // false 0
/[a-z]/.test(pwd) && /[A-Z]/.test(pwd) && /\d/.test(pwd) && /^[a-zA-Z0-9]{6,}$/.test(pwd);
/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z0-9]{6,}$/.test(pwd)
Detailed graphic explanation of using regular multi-line mode and single-line mode
Based on PHP regular zero-width Detailed explanation of the use of assertions
The above is the detailed content of Detailed explanation of the use of lastIndex in regular expressions. For more information, please follow other related articles on the PHP Chinese website!