regex从javascript中删除多个逗号和空格。

时间:2021-06-15 21:41:57

I have a string like

我有一根像绳子的

var str=" , this,  is a ,,, test string , , to find regex,,in js.  , ";

in which there are multiple spaces in beginning,middle and end of string with commas. i need this string in

其中,字符串的开头、中间和结尾都有多个空格和逗号。我需要这根绳子

var str="this is a test string to find regex in js.";

i found many regex in forum removing spaces , commas separately but i could not join them to remove both.

我在论坛中发现了很多regex,分别是逗号,但我不能同时删除它们。

Please give explanation of regex syntex to if possible .

如果可能,请解释一下regex syntex。

Thanks in advance

谢谢提前

4 个解决方案

#1


17  

You can just replace every space and comma with space then trim those trailing spaces:

你可以把每个空格和逗号都换成空格,然后把后面的空格去掉:

var str=" , this,  is a ,,, test string , , to find regex,,in js.  , ";
res = str.replace(/[, ]+/g, " ").trim();

jsfiddle demo

jsfiddle演示

#2


1  

you can use reg ex for this

你可以用reg ex

/[,\s]+|[,\s]+/g

var str= "your string here";
//this will be new string after replace
str = str.replace(/[,\s]+|[,\s]+/g, 'your string here');

RegEx Explained and Demo

正则表达式解释和演示

#3


1  

Try something like this:

试试这样:

var new_string = old_string.replace(/[\s,]+/g,' ').trim();

The regex is simply [\s,]+ if we were to break this down the \s means ANY whitespace character and , is a literal comma. The [] is a character set (think array) and the + means one or more matches.

regex仅仅是[\s,]+如果我们将其分解,那么s表示任何空格字符,并且是一个字面逗号。[]是一个字符集(think array), +表示一个或多个匹配。

If you just wanted a space and not any whitespace, you could replace the \s with a literal space as well.

如果你只想要一个空格而不是任何空格,你也可以用一个文字空格来替换\s。

We throw in a /g on the end so that it does a global search and replace, otherwise it'd just do it for one match only.

我们在末尾加入一个/g这样它就可以进行全局搜索和替换,否则它只会对一个匹配进行替换。

#4


0  

You should be able to use

你应该能够使用

str.replace(/,/g," ");

The 'g' is the key, you may need to use [,]

g是键,你可能需要使用[,]

#1


17  

You can just replace every space and comma with space then trim those trailing spaces:

你可以把每个空格和逗号都换成空格,然后把后面的空格去掉:

var str=" , this,  is a ,,, test string , , to find regex,,in js.  , ";
res = str.replace(/[, ]+/g, " ").trim();

jsfiddle demo

jsfiddle演示

#2


1  

you can use reg ex for this

你可以用reg ex

/[,\s]+|[,\s]+/g

var str= "your string here";
//this will be new string after replace
str = str.replace(/[,\s]+|[,\s]+/g, 'your string here');

RegEx Explained and Demo

正则表达式解释和演示

#3


1  

Try something like this:

试试这样:

var new_string = old_string.replace(/[\s,]+/g,' ').trim();

The regex is simply [\s,]+ if we were to break this down the \s means ANY whitespace character and , is a literal comma. The [] is a character set (think array) and the + means one or more matches.

regex仅仅是[\s,]+如果我们将其分解,那么s表示任何空格字符,并且是一个字面逗号。[]是一个字符集(think array), +表示一个或多个匹配。

If you just wanted a space and not any whitespace, you could replace the \s with a literal space as well.

如果你只想要一个空格而不是任何空格,你也可以用一个文字空格来替换\s。

We throw in a /g on the end so that it does a global search and replace, otherwise it'd just do it for one match only.

我们在末尾加入一个/g这样它就可以进行全局搜索和替换,否则它只会对一个匹配进行替换。

#4


0  

You should be able to use

你应该能够使用

str.replace(/,/g," ");

The 'g' is the key, you may need to use [,]

g是键,你可能需要使用[,]