Home > Web Front-end > JS Tutorial > Javascript regular expression implementation to add thousand separators to numbers_javascript skills

Javascript regular expression implementation to add thousand separators to numbers_javascript skills

WBOY
Release: 2016-05-16 16:10:41
Original
1439 people have browsed it

Recently, I saw an interview (written test) question on the Internet about using js to implement thousands separators for numbers, so I wrote a method using "regular replace" to implement it:

Copy code The code is as follows:

var thousandBitSeparator = function(numStr){
var b = /([- ]?d{3})(?=d)/g;
Return numStr.replace(b, function($0, $1){
           return $1 ',';
});
}

Supports positive and negative sign matching and decimal point distinction. If there are any errors, please point them out:-D

Attached is another netizen’s implementation method

Copy code The code is as follows:


The above is the entire content of this article, I hope you all like it.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template