简体   繁体   English

从javascript中的字符串中删除第二个逗号

[英]remove second comma from a string in javascript

I have an string as: 我有一个字符串为:

0123456789,, 0987654213 ,, 0987334213,, ......

How can I convert this into 我如何将其转换为

0123456789, 0987654213, 0987334213, ......

ie I want to remove the second comma 即我想删除第二个逗号

You can do it very simply, like this using regex. 您可以非常简单地执行此操作,例如使用正则表达式。

var str = "0123456789,,0987654213,,0987334213,,,,,9874578";
str=str.replace(/,*,/g,',');
console.log(str)
var str = "0123456789,, 0987654213 ,, 0987334213,, ......"
console.log(str.replace(/\,+/g,","));

You can use replace() method with regular expression with g flag to replace all instances ',,' with ',': 您可以在带有g标志的正则表达式中使用replace()方法,以将所有实例“,”替换为“,”:

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

Here's a simple example 这是一个简单的例子

 var str = '0123456789,, 0987654213 ,, 0987334213'; str = str.replace(/,,/g, ","); console.log(str); 

这将用单个逗号替换所有出现的连续逗号:

str.replace(/,+/g, ',');

 var str = "0123456789,, 0987654213 ,, 0987334213,," str = str.split(",,").join(",") console.log(str); 

There is a replace method for String. 字符串有一个替换方法。 You can replace ',,' with a ','. 您可以将“,”替换为“,”。

An example: 一个例子:

var str = "0123456789,, 0987654213,, 0987334213,"; 
var newStr = str.replace(/,,/g,','));

The output: 输出:

0123456789, 0987654213, 0987334213,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM