How do I replace single quotes with double quotes in JavaScript?

var a = "[{'column1':'value0','column2':'value1','column3':'value2'}]";
var b = a.replace(/'/g, '"');
console.log(b);

Edit: Removed \ as there are useless here.


Need to use regex for this:

var a = "[{'column1':'value0','column2':'value1','column3':'value2'}]";
var b = a.replace(/\'/g, "\"");

http://jsfiddle.net/9b3K3/


You can use a global qualifier (a trailing g) on a regular expression:

var b = a.replace(/'/g, '"');

Without the global qualifier, the regex (/'/) only matches the first instance of '.

Tags:

Javascript