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 :)
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, "'") ))
You can do:
" with ' in for all the matched results