Analysis of lastIndex and pre-query and indexof Regular Expressions in Regular Expressions

Source: Internet
Author: User

Analysis of lastIndex and pre-query and indexof Regular Expressions in Regular Expressions

Write the following output in sequence.

var reg1 = /a/;var reg2 = /a/g;console.log(reg1.test('abcabc')); // trueconsole.log(reg1.test('abcabc')); // trueconsole.log(reg1.test('abcabc')); // trueconsole.log(reg1.test('abcabc')); // trueconsole.log(reg2.test('abcabc')); // trueconsole.log(reg2.test('abcabc')); // trueconsole.log(reg2.test('abcabc')); // falseconsole.log(reg2.test('abcabc')); // true

A simple regular expression is used to test whether the string abcabc contains character. However, a special false exists in the result, Why?

LastIndex (for regular expressions with parameter g)

In each instantiated RegExp object, a lastIndex attribute exists, and its initial value is 0.

//. LastIndex // 0new RegExp ('A '). lastIndex // 0lastIndex indicates that when the match is successful, the position + 1 in the original string where the last character of the match content is located, that is, the index of the next character that matches the content (if the match content is at the end of the string, returns the position + 1 in the original string, that is, the length of the string ). If the parameter g is not included, lastIndex is always 0. Var reg =/AB/g; reg. test ('123abc'); console. log (reg. lastIndex) // 5 // match the content in the final var reg =/AB/g; reg. test ('123ab'); console. log (reg. lastIndex) // 5 // without the gvar reg =/AB/; reg. test ('123abc'); console. log (reg. lastIndex) // 0

The lastIndex is the starting position of matching when this regular expression is used for other matching operations. When the matching fails, the value of lastIndex is reset to 0.

Var reg =/AB/g; // The initial value is 0. The matching is successful from the beginning, and lastIndex is 4console. log (reg. test ('12ab34ab'), reg. lastIndex); // true 4 // The Matching content starts from the 4th character "3" and the second AB lastIndex is 8console. log (reg. test ('12ab34ab'), reg. lastIndex); // true 8 // starting from 8th bits (the character length is 8, there are no 8th bits), matching fails to be successfully reset to 0console. log (reg. test ('12ab34ab'), reg. lastIndex); // false 0 // match the console from the beginning. log (reg. test ('12ab34ab'), reg. lastIndex); // true 4

The answer to this question is complete, and the next step is expansion.

For those that have not been re-declared, reg is prone to mistakes.

// Test whether the string str1 and str2 contain the AB character var reg =/AB/g; var str1 = '123ab'; var str2 = 'ab123'; console. log (reg. test (str1); // trueconsole. log (reg. test (str2); // false

Obviously, this error is caused by lastIndex. Here, you can modify reg without the g parameter or re-declare reg. Of course, you can also manually modify reg. lastIndex = 0 after the first match.

Pre-Check

Next, the pre-query literally means the pre-query, that is, the following content of the query Matching content. However, the pre-query matches the pre-query content and does not return any results.

We often need to match some characters in the string with some characters, but we do not need to include the following characters in the matching result, for example:

Find all the characters in the following string followed by 2.

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. Here we only need the characters. Pre-check:

'a1b2c22d31e4fg6h2'.match(/[a-z](?=2)/g); // ["b", "c", "h"]

We can see that the conditions are fully met, but what is the relationship between the pre-query and the topic lastIndex in this article?

Let's use test to see why test is used. here we need to explain that match matches all until the match ends when the match is unsuccessful and the match fails, the lastIndex is reset to 0.

Exec and test are returned if the first matching is successful or the matching fails.

var reg1 = /[a-z](?=2)/g;var reg2 = /[a-z]2/g;var str = 'a1b2c22d31e4fg6h2';console.log(reg1.test(str), reg1.lastIndex); // true 3console.log(reg1.test(str), reg1.lastIndex); // true 5console.log(reg1.test(str), reg1.lastIndex); // true 16console.log(reg1.test(str), reg1.lastIndex); // false 0console.log(reg2.test(str), reg2.lastIndex); // true 4console.log(reg2.test(str), reg2.lastIndex); // true 6console.log(reg2.test(str), reg2.lastIndex); // true 17console.log(reg2.test(str), reg2.lastIndex); // false 0

Are there any problems? The pre-query lastIndex does not contain the pre-query content! This can be used to simplify many judgments.

For example, to match a password, you must have at least one uppercase letter, one lowercase letter, one number, at least six characters in length, and can only be a combination of numbers and letters.

The following statement is used to determine if no pre-query is performed:

/[a-z]/.test(pwd) && /[A-Z]/.test(pwd) && /\d/.test(pwd) && /^[a-zA-Z0-9]{6,}$/.test(pwd);

However:

/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z0-9]{6,}$/.test(pwd)

Split it and you can see:

(? =. * [A-z]) Whether there are lower-case letters, but whether the pre-query fails, false is returned. If the lastIndex is successful, the value is 0. Similarly, we can understand the pre-query content, the last step is the combination of letters, numbers, and more from 6 to 6, but the first step is pre-check. lastIndex is always not 0, and each match starts from the first match, so it meets the requirements.

The above section describes lastIndex and pre-check in regular expressions. I hope it will be helpful to you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.