css - Regular Expression - replace urls in style tags -


i have searched on google , stackoverflow didnt find answer. tryed myself, iam no regex guru.

my goal replace relative urls in html style tag absolute version.

e.g.

  • style="url(/test.png)" style="url(http://mysite.com/test.png)"
  • style="url("/test.png")" style="url("http://mysite.com/test.png")"
  • style="url('/test.png')" style="url('http://mysite.com/test.png')"
  • style="url(../test.png)" style="url(http://mysite.com/test.png)"
  • style="url("../test.png")" style="url('http://mysite.com/test.png')"
  • style="url('../test.png')" style="url('http://mysite.com/test.png')"

and on.

here tryed poor regex "skils"

url\((?<url>[^\)]*)\) 

gives me url in "url" function.

thanks in advance!

well, can try regex:

style="url\((['"])?(?:\.\.)?(?<url>[^'"]+)\1?\)" 

and replace with:

style="url($1http://mysite.com$2$1)" 

regex101 demo

(['"])? capture quotes if present , use them again @ \1?

([^'"]+) capture url itself.


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

c++ - Clear the memory after returning a vector in a function -

erlang - Saving a digraph to mnesia is hindered because of its side-effects -