Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

211
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

0

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

about 4 years ago · Juan Pablo Isaza Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda