简体   繁体   中英

Replace comma by double quote in javascript

How to replace comma by double quote in javascript? For example: "c,C#,JavaScript" should be like "C","C#","JavaScript"

str = '"c,C#,JavaScript"';
str = str.split(',').join('","');

That would result in "c","C#","JavaScript"

var original = '"c,C#,JavaScript"';

var quoted = original.replace(/,/g, '","');    // "c","C#","JavaScript"

Just to toss it in there, you could also .split() and .join() , like this:

var oldString = '"c,C#,JavaScript"';
var newString = oldString.split(',').join('","');

You can test it here .

You can do this:

str = "c,C#,JavaScript";
str = str.replace(/,/g, '"');

Result:

c"C#"JavaScript

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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