Sometimes, we want to replace single quotes with double quotes in JavaScript.
In this article, we’ll look at how to replace single quotes with double quotes in JavaScript.
How to replace single quotes with double quotes in JavaScript?
To replace single quotes with double quotes in JavaScript, we use the string replace method.
For instance, we write
const a = "[{'column1':'value0','column2':'value1','column3':'value2'}]";
const b = a.replace(/\'/g, '"');
to call a.replace with a regex that replace all single quotes with double quotes.
We use \' to match single quotes.
The g flag makes replace replace all matches.
The string with the replacements done is returned.
Conclusion
To replace single quotes with double quotes in JavaScript, we use the string replace method.