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

218
Vistas
How to replace a specific char in a substring of a long text?

From javascript, given this multi-lines text (stocked in a variable):

+++ diagramme
graph TD
    A[fooA] ==> B[fooB]
    style A color:blue
    B --> C[(<img src="https://c7.alamy.com/compfr/jcdcx7/image-d-icone-de-soleil-jcdcx7.jpg" width="50">)]
    C --> D[fooD]
    B --> D
    click D "https://fii.com" _blank
+++

I'm looking for a way to replace all " by the char ' within the img tag. In the img tag, the number of attributes can vary.

In my example, the result expected is :

+++ diagramme
graph TD
    A[fooA] ==> B[fooB]
    style A color:blue
    B --> C[(<img src='https://c7.alamy.com/compfr/jcdcx7/image-d-icone-de-soleil-jcdcx7.jpg' width='50'>)]
    C --> D[fooD]
    B --> D
    click D "https://fii.com" _blank
+++

I know how use regex (thanks amazing website like https://regex101.com/ ! ), so I can capture the img tag with this simple pattern /(<img)(.*)(>)/g . But I don't know how I can replace all occurences of a specific char (here ") by a another (here ') in a substring obtained via a regex.

edit : the position and the number of the img tags can vary in the structure '+++ diagramme ... +++'. Below an another example :

+++ diagramme
graph TD
    A[fooA] ==> B[(<img src="https://fuu/image1.jpg" width="50">)]
    B --> C[fooC]
    click B "https://faa.com" _blank
    C --> D[(<img src="https://fuu/image2.jpg">)]
+++

and the expected result :

+++ diagramme
graph TD
    A[fooA] ==> B[(<img src='https://fuu/image1.jpg' width='50'>)]
    B --> C[fooC]
    click B "https://faa.com" _blank
    C --> D[(<img src='https://fuu/image2.jpg'>)]
+++

Thanks for any help :)

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Use a callback function:

const test = `+++ diagramme
graph TD
    A[fooA] ==> B[fooB]
    style A color:blue
    B --> C[(<img src="https://c7.alamy.com/compfr/jcdcx7/image-d-icone-de-soleil-jcdcx7.jpg" width="50">)]
    C --> D[fooD]
    B --> D
    click D "https://fii.com" _blank
+++`
console.log(test.replace(/<img[^>]*>/g, (x) => x.replace(/"/g, "'") ))

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can do:

  1. Getting all the image tags using RegExp
  2. Replacing the " with ' in for all the matched results
  3. Replace the image tags with the fixed image tags or you can create 2 substrings, before the match and after the match + match length and concat it with the fixed image (don’t forget to fix the indexes of the next image match if the new image tag is in different length than the first image tag)
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