Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Make a regular expression in the search in VS code

I’d like to change the imghtml tag to django html with {%static
Can you make a regular expression in the ctrl + f function so I can change it at once?

ex:

<img src="../img/logo.png" => <img src=" {%static 'img/logo.png' %}"

r'<img\s+src="(.*?)"'

But I can’t find a way to change the code while preserving the middle values

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can work with a backreference:

search query: <img\s+src="../([^"]*)"
replace with: <img src="{% static '$1' %}"

The $1 will then be replaced with the first capture group.

That being said, HTML is a context-free language. That means most tasks can not be handled (perfectly) with a regular expression. For example the regex we here make does not take into accounts attributes before the src="" attribute. Likely a tool like BeautifulSoup is therefore a better idea.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading