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