Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

226
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda