Regular analysis--add a comma to the left of the floating point points every three bits

Source: Internet
Author: User

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

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.