48

The following code replaces only one single quote:

var a = "[{'column1':'value0','column2':'value1','column3':'value2'}]";
var b = a.replace("'", "\"");
console.log(b);
Piper
  • 1,220
  • 2
  • 15
  • 25
GaneshT
  • 3,731
  • 13
  • 50
  • 81

7 Answers7

117

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

Edit: Removed \ as there are useless here.

mitesh7172
  • 606
  • 10
  • 20
RafH
  • 4,396
  • 2
  • 23
  • 23
8

Need to use regex for this:

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

http://jsfiddle.net/9b3K3/

RaphaelDDL
  • 4,369
  • 2
  • 31
  • 52
4

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 '.

Ted Hopp
  • 227,407
  • 48
  • 383
  • 507
4

replaceAll(search, replaceWith) replaces ALL occurrences of search with replaceWith.

Then, make sure you have a string by wrapping one type of qoutes by different type:

 "a 'b' c".replaceAll("'", '"')
 // result: "a "b" c"
    
 "a 'b' c".replaceAll(`'`, `"`)
 // result: "a "b" c"

More about replaceAll

replaceAll (MDN): replaceAll(search, replaceWith)

It's actually the same as using replace() with a global regex(*), merely replaceAll() is a bit more readable in my view.

(*) Meaning it'll match all occurrences.


Example 1 - search with a string

const p = 'Please replace all 2020 occurrences with 2021. 2020. 2020.' 
   
console.log(p.replaceAll('2020', '2021'));
// Result: "Please replace all 2021 occurrences with 2021. 2021. 2021."

Example 2 - search with regex

const p = 'Please replace all 2020 occurrences with 2021. 2020. 2020.'    
const regex = /2020/gi


console.log(p.replaceAll(regex, '2021'));
// Result: "Please replace all 2021 occurrences with 2021. 2021. 2021."

Important(!) if you choose regex:

when using a regexp you have to set the global ("g") flag; otherwise, it will throw a TypeError: "replaceAll must be called with a global RegExp".


You can also use a function as replaceWith

In this case, the function will be invoked after the match has been performed. The function's result (return value) will be used as the replacement string.

Raz
  • 344
  • 3
  • 6
3

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);
bfavaretto
  • 70,503
  • 15
  • 107
  • 148
  • I did that but it takes too long than replacing the string. – GaneshT May 08 '13 at 23:37
  • I could have created the JSON string in proper format in the first place, but I was having trouble storing it (as session) in a hidden field. I replaced all the double quote to single quote so it would get stored in the hidden field. Then on the client side I'm replacing it back to double quote. I would appreciate if there is a better solution for it – GaneshT May 08 '13 at 23:39
0

Add the g modifier: var b = a.replace(/'/g, '"');

sebnukem
  • 7,673
  • 5
  • 36
  • 48
0

You can use a RegExp with the global flag g and all quotes will replaced:

var b = a.replace(/'/g, '"');
yckart
  • 30,290
  • 9
  • 117
  • 125
  • You don't "just add the global flag `g`". You created a regular expression and set the global flag. Which, by, the way, replaces the wrong characters with the wrong character – Ian May 08 '13 at 21:14