Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Replacing single quote from a string

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading