I have a string with contain some single quote, I want to replace all the single quote present inside string with double quote excluding the single quote preset inside {} [] () . Any pointer will be really helpful trying to solve this using JAVASCRIPT.
Actual String :
let str =`name:'John' {hobbies:'Playing'} , (age: '45')`;
console.log(str);
Expected Output:
name:"John" {hobbies:’Playing’} , (age: ’45’)
>Solution :
Check out .replaceAll.
In order to replace just the single quotes around John, you’ll probably want to use a bit of Regex.
let str = "name:'John' {hobbies:'Playing'} , (age: '45')";
str = str.replaceAll(/(name:)'([^']+?)'/g, "$1\"$2\"");
console.log(str);
// name:"John" {hobbies:'Playing'} , (age: '45')