1

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

4
  • Are the outer quotes part of your value? And how did "c" get capitalized to "C"? Can you show some code that demonstrates what you're trying to do? It looks like you really only need split... Commented Dec 7, 2010 at 10:15
  • @kobi sorry actually it was typo. Commented Dec 7, 2010 at 10:22
  • Just to clarify: do you want to change a,b,c to a","b","c? Commented Dec 7, 2010 at 10:22
  • Thanks to all for your reply and +1 from me to all. Commented Dec 7, 2010 at 10:23

4 Answers 4

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

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

Sign up to request clarification or add additional context in comments.

Comments

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

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

Comments

3

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.

1 Comment

@Gert - I'd say it is defined very poorly by the OP, but it follows the specs.
1

You can do this:

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

Result:

c"C#"JavaScript

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.