Home  >  Article  >  Web Front-end  >  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
WBOYOriginal
2016-05-16 16:10:411413browse

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.

Statement:
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