The following code replaces only one single quote:
Need to use regex for this:
var a = "[{'column1':'value0','column2':'value1','column3':'value2'}]";
var b = a.replace(/\'/g, "\"");
http://jsfiddle.net/9b3K3/
Add the g
modifier:
var b = a.replace(/'/g, '"');
You can use a RegExp with the global flag g
and all quotes will replaced:
var b = a.replace(/'/g, '"');
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 '
.
This looks suspiciously like bad JSON, so I suggest using actual array and object literals, then encoding the proper way:
var a = [{'column1':'value0','column2':'value1','column3':'value2'}];
var b = JSON.stringify(a);
var a = "[{'column1':'value0','column2':'value1','column3':'value2'}]";
var b = a.replace(/'/g, '"');
console.log(b);
Edit: Removed \ as there are useless here.