Before encountering regular expressions, difficult are not willing to analyze, always feel that after touching, but as a programmer, is very important, here to remember this example.
First, a usage of the regular:(? =pattern)
Positive pre-check to match the find string at the beginning of any string matching pattern.
This is a non-fetch match, which means that the match does not need to be acquired for later use.
For example,"Windows (?= | 98| Nt| ) " Can match "Windows" in "Windows2000", but cannot match "Windows3." 1"Windows" in.
The code is as follows:
function Commafy (num) { return num && num . toString () . replace (function {$ 1, $ console.log, $ $); return $ + ', '; }); Console.log (Commafy (
Output:
Analysis:
1.$1, $ $, $ $ each represents the value of a regular match in parentheses
(\d{3}) +\. Description matches 3 and 3 times the number of digits plus "." End of as 345678. Or 678.
(\d) (? = (\d{3}) +\. Indicates that only 3 and 3 times the number of digits plus "." When matched to that number. The end of the number is matched as 2345678. The 2 is matched to 5678. The 5 are matched
Regular analysis--add a comma to the left of the floating point points every three bits