JavaScript Regular (converts a number to a three-bit delimited style) "Go"

Source: Internet
Author: User

Original: https://www.cnblogs.com/sivkun/p/7123963.html

' 12345678912345678 '. Replace (/\b (? = (?: \ d{3',')

Explain:

\b: Matches the word boundary, which is the position between the character \w ([a-za-z0-9_]) and \w[^a-za-z0-9_], or between the character \w and the beginning or end of the string.

\b: matching non-word boundaries

varReg =/\b\d/G;varstr ='123'; reg.exec (str);//["1", Index:0,input: "123"]Reg.exec (str);//NULLReg=/\d\b/g;reg.exec (str);//["3", Index:2,input: "123"]Reg.exec (str);//NULLReg=/\b\d/g;reg.exec (str);//["2", index:1, Input: "123"]Reg.exec (str);//["3", Index:2, Input: "123"]Reg.exec (str);//NULLReg=/\d\b/g;reg.exec (str);//["1", index:0, Input: "123"]Reg.exec (str);//["2", index:1, Input: "123"]Reg.exec (str);//NULL

With the code above you can understand what \b and \b really are.

' (?: EXP) ': Regular expression parentheses have a dual effect of grouping and capturing, and if you start with '?: ' In the small width, you can make it non-capturing.
' Exp1 (? =exp2) ': Forward-looking (0-wide forward assertion, 0-wide means (? =exp2) does not occupy position, just represents an expectation), to match the EXP1 to be satisfied is followed by EXP2
' Exp1 (?! EXP2) ': Negative forward (0-wide negative lookahead assertion), to match the EXP1 to meet the back is not EXP2


varstr ='Hello, how are you ?';varReg =/good (? = AH)/g;reg.exec (str);//["OK", index:5, Input: "How are you, hello?"]Reg.exec (str);//NULLReg =/Good (?! AH)/; reg.exec (str);//["OK", index:1, Input: "How are you, hello?"]Reg.exec (str);//["OK", index:9, Input: "How are you, hello?"]Reg.exec (str);//NULL//explain the 0 wide, below this regular means ' you ' behind the ' good ' to match, expect the back is ' good '! //but the back is followed by a ' \d ', Understanding the ' (? = good) ' Ignore (because it is 0 wide, does not occupy position),//This means that you have to match a number in the back of ' you ', which contradicts the expectations above .//so this regular does not match any strings. Reg =/You (? = good) \d/; reg.exec (str);//NULLReg.test (str);//falseReg.test ('Hello');//falseReg.test ('Hello 1');//falseReg.test ('you're 1 good.');//false

Look again '/\b (? = (?: \ D{3}) +\b)/g ' What do you mean
1. First, the global match
2. Match non-word boundaries
3. Non-word boundaries are followed (3 consecutive integers are numbered and word boundaries are added)
4. Continuous numbers are not captured
"123456789". Replace (/\b (? = (?: \ D{3}) +\b)/g, ', ') '
Execution process:
The Replace function makes multiple matches, and \b represents the word boundary to match.
1. First start from the middle of 1 and 2 but there are 8 numbers in the back that do not meet the forward-looking conditions
2. Then to the middle of 2 and 3, likewise not satisfied
3. To the middle of 3 and 4, the following 6 digits are met
4. Replace the \b (non-word boundary) between 3 and 4 with ",".
5. Match the replacement in turn
6. Final return of the new string "123,456,789"

The regular is very powerful, more simple and convenient than the conventional method of implementation.

JavaScript Regular (converts a number to a three-bit delimited style) "Go"

Related Article

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.