JS regular match ID number has pit

Source: Internet
Author: User

Can not add g, each match will be lastindex as the starting bit to find//flocculent G, match to the last index to change the lastindex, no match to the lastindex will be reset to 0//no g,lastindex is always 0var Textarr = [    ' 430993199501019207 ',    ' 43099319950101920X ',    ' 43099319950101920X ',    ' 1234567890123456 ' ,    ' 430993199501019 ',    ' 430993 ',    ' x430993199501019x ',]var regex =/^ (\d{15}| ( \D{17}[\DXX]) $/var result = [True, True, True, False, True, False, False]textarr.foreach (val, idx) = = {    Console . log (Val, val.length,  Regex.test (Val), Result[idx], Regex.test (val)! = Result[idx]? ' Failed ': ' Verify Success ')})

  

When you create a regular expression object that uses the "G" identifier or sets its global property value to Ture, the newly created regular expression object will use the pattern to match the string that will match. You can perform multiple matches on a string that you specify to find in global match mode. Each match uses the value of the Lastindex property of the current regular object as the starting position to start the lookup in the target string. The initial value of the Lastindex property is 0, and when a matching item is found, the value of lastindex is reset to the position index of the next character in the string for the match, to identify where the lookup will start the next time the match is performed, and if no matching item is found lastindex the value is set to 0. When the global match flag for a regular object is not set, the value of the Lastindex property is always 0, and each execution match finds only the first matching item in the string. The following code can be used to view the value of the corresponding Lastindex property in the execution match, the code is as follows:

var str = "123#ABC"; var re =/abc/ig; Console.log (Re.test (str)); Output ture Console.log (Re.lastindex); Output 7 Console.log (Re.test (str)); Output False Console.log (Re.lastindex); Output 0 console.log (re.test (str)); Output ture Console.log (Re.lastindex); Output 7 Console.log (Re.test (str)); Output False Console.log (Re.lastindex); Output 0

  

JS regular match ID number has pit

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.