JQuery Regex想要从字符串中删除逗号

时间:2021-10-28 21:36:05

I have a string like

我有一个字符串

Andrew,Lucy,,David,Tess,

I want to remove ,, after Lucy and also a comma from the end of the string so the end results i need would be

我想删除,在Lucy之后,还有一个逗号从字符串的末尾,所以我需要的最终结果

Andrew,Lucy,David,Tess

is there any way to achieve this in jQuery?

有没有办法在jQuery中实现这一点?

5 个解决方案

#1


2  

Use Positive Lookahead ?=

使用正向前瞻?=

var str = "Andrew,Lucy,,David,Tess,";
str = str.replace(/,(?=,+|$)/g, "");
console.log( str ); // Andrew,Lucy,David,Tess

stop at comma , than lookahead ?= for it's followed by:
,+ one or more commas
| or it's at end of string $

停在逗号,而不是向前看?=然后是:,+一个或多个逗号|或者它在字符串$的末尾

Important

Most probably you'll need to take care for cases when the string starts with one or more commas, than simply append ^,+| to your regex:

很可能你需要注意字符串以一个或多个逗号开头的情况,而不是简单地追加^,+ |你的正则表达式:

var str = ",,Andrew,Lucy,,David,Tess,,";
str = str.replace(/^,+|,(?=,+|$)/g, "");
console.log( str ); // Andrew,Lucy,David,Tess

or respectively /^,+|,(?=,+)|,+$/g

或者分别是/ ^,+ |,(?=,+)|,+ $ / g

#2


2  

This is a working regex:

这是一个正在运行的正则表达式:

/,+(?=,|$)/g

Online Demo

Remove redundant commas followed by a comma or end of line. If you apply this to multiple lines add m (multiline flag) after the closing slash too (otherwise $ matches only at the very end of the text instead of at each newline).

删除多余的逗号,后跟逗号或行尾。如果将此应用于多行,则在结束斜杠后添加m(多行标记)(否则$仅匹配文本的最末端而不是每个换行符)。

Code Demo

// Add /i at bottom to make the regex it case insensitive
var re = /,+(?=,|$)/g; 

var tests = ['Andrew,Lucy,,David,Tess,','Andrew,,,,Lucy,,,David,Tess,,,'].reverse();
var m;

while( t = tests.pop() ) {
    document.write('"' + t + '" => <font color="green">' + t.replace(re,'') + '</font><br/>');
}

#3


0  

Thanks all. Really appreciate your efforts.

谢谢大家。真的很感激你的努力。

Did something like this. It worked

做过类似的事。有效

var your_string = 'Andrew, Lucy,, David, Tess,';
newstr=your_string.replace(',,', ',');

your_string1 = newstr.replace(/[,]$/,'');

first i removed double commas and then i removed comma from the end

首先我删除了双逗号然后我从最后删除了逗号

#4


-1  

Or You can simply use .replace()

或者你可以简单地使用.replace()

var str = "Andrew,Lucy,,David,Tess,";
var res = str.replace(",,", ",").replace(/,\s*$/, ""); 

Working Demo

#5


-1  

Without a regex :

没有正则表达式:

var str = "Andrew,Lucy,,David,Tess,";
var res = str.split(",");
res = res.filter(Boolean);
res = res.join(",");
console.log(res);

With a regex and replace function():

使用正则表达式和替换函数():

var str = "Andrew,Lucy,,David,Tess,";
var res = str.replace(/,,|,$/g, function(x){
  return x == ",," ? ",": "";});
console.log(res);

#1


2  

Use Positive Lookahead ?=

使用正向前瞻?=

var str = "Andrew,Lucy,,David,Tess,";
str = str.replace(/,(?=,+|$)/g, "");
console.log( str ); // Andrew,Lucy,David,Tess

stop at comma , than lookahead ?= for it's followed by:
,+ one or more commas
| or it's at end of string $

停在逗号,而不是向前看?=然后是:,+一个或多个逗号|或者它在字符串$的末尾

Important

Most probably you'll need to take care for cases when the string starts with one or more commas, than simply append ^,+| to your regex:

很可能你需要注意字符串以一个或多个逗号开头的情况,而不是简单地追加^,+ |你的正则表达式:

var str = ",,Andrew,Lucy,,David,Tess,,";
str = str.replace(/^,+|,(?=,+|$)/g, "");
console.log( str ); // Andrew,Lucy,David,Tess

or respectively /^,+|,(?=,+)|,+$/g

或者分别是/ ^,+ |,(?=,+)|,+ $ / g

#2


2  

This is a working regex:

这是一个正在运行的正则表达式:

/,+(?=,|$)/g

Online Demo

Remove redundant commas followed by a comma or end of line. If you apply this to multiple lines add m (multiline flag) after the closing slash too (otherwise $ matches only at the very end of the text instead of at each newline).

删除多余的逗号,后跟逗号或行尾。如果将此应用于多行,则在结束斜杠后添加m(多行标记)(否则$仅匹配文本的最末端而不是每个换行符)。

Code Demo

// Add /i at bottom to make the regex it case insensitive
var re = /,+(?=,|$)/g; 

var tests = ['Andrew,Lucy,,David,Tess,','Andrew,,,,Lucy,,,David,Tess,,,'].reverse();
var m;

while( t = tests.pop() ) {
    document.write('"' + t + '" => <font color="green">' + t.replace(re,'') + '</font><br/>');
}

#3


0  

Thanks all. Really appreciate your efforts.

谢谢大家。真的很感激你的努力。

Did something like this. It worked

做过类似的事。有效

var your_string = 'Andrew, Lucy,, David, Tess,';
newstr=your_string.replace(',,', ',');

your_string1 = newstr.replace(/[,]$/,'');

first i removed double commas and then i removed comma from the end

首先我删除了双逗号然后我从最后删除了逗号

#4


-1  

Or You can simply use .replace()

或者你可以简单地使用.replace()

var str = "Andrew,Lucy,,David,Tess,";
var res = str.replace(",,", ",").replace(/,\s*$/, ""); 

Working Demo

#5


-1  

Without a regex :

没有正则表达式:

var str = "Andrew,Lucy,,David,Tess,";
var res = str.split(",");
res = res.filter(Boolean);
res = res.join(",");
console.log(res);

With a regex and replace function():

使用正则表达式和替换函数():

var str = "Andrew,Lucy,,David,Tess,";
var res = str.replace(/,,|,$/g, function(x){
  return x == ",," ? ",": "";});
console.log(res);