Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

209
Views
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:

name:"John" {hobbies:'Playing'} , (age: '45')

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

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

about 4 years ago · Juan Pablo Isaza Report

0

You can use the method str.replaceAll("'", '"');

about 4 years ago · Juan Pablo Isaza Report

0

let braceTracker = 0;

// convert string into an array of string using spread operator
// alternatively you can do str.split("")
let str = [..."name:'John' {hobbies:'Playing'} , (age: '45')"];

for (let x = 0; x < str.length; x++)
{
    if(str[x] === "[" || str[x] === "{" || str[x] === "(")
        braceTracker++;
    else if(str[x] === "]" || str[x] === "}" || str[x] === ")")
        braceTracker--;
    else if(str[x] === "'" && braceTracker === 0)
        str[x] = "\"";
}

console.log(str.join(""));

// output :   name:"John" {hobbies:'Playing'} , (age: '45')
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!