Home > Web Front-end > JS Tutorial > body text

js 中将多个逗号替换为一个逗号的代码_javascript技巧

WBOY
Release: 2016-05-16 16:45:22
Original
1294 people have browsed it

有时候有很多逗号,这样我们就不好处理了,下面的函数就是将多个逗号替换为一个逗号,方便后面的处理。

复制代码 代码如下:




比较不错的一个代码:

复制代码 代码如下:

function dostr(str){
str=trim(str);
var strarry=unique(str.split(","));
str=strarry.join(",");
str=str.replace(/,/ig,",");
str=str.replace(/[^0-9,]*/ig,"");
str=str.replace(new RegExp(',+',"gm"),',');
if (str.substr(0,1)==',') str=str.substr(1);
var reg=/,$/gi;
str=str.replace(reg,"");
return str;
}
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!