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

219
Views
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 answers
Answer question

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 Report

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 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!