3

I'm using nginx with module HttpSubsModule to replace a few css stylesheets for another. My regex is

subs_filter '<link(.*)href="(.+\.css)([\?.]?[^"]*)(".*)>' '<link  href="new.css" rel="stylesheet" type="text/css">' irg;

It seems valid but subs_filter doesn't work with it. Simpler forms, such as

subs_filter (.*)css '<link  href="new.css" rel="stylesheet" type="text/css">' irg;

do work but catch a lot of false results.

How should I write this regex?

EDIT: The following version works on www.regexe.com replacing any link tag with a css file, but still doesn't work on nginx.

subs_filter '(<link.*href=")(.+\.css)([\?.]?[^"]*)(".*>)' '$1new.css$3$4' irg;
4
  • The docs say that it will not work with proxy compressed response. You can disable the compressed response like this: proxy_set_header Accept-Encoding "";. Does it help? Commented May 25, 2015 at 19:11
  • I've tried to be sure but no. Note that I can make some basic substitution, just not with the complexity I'd like. Commented May 25, 2015 at 19:15
  • Try with this regex: (<link.*?href=")(.+?\.css)([\?.]?[^"]*?)(".*?>). I think the reason might be in the regex greediness. Commented May 25, 2015 at 19:34
  • Nothing happens. So far I got this working: subs_filter href="(.*)css.*" test ir; but it is too broad. Commented May 25, 2015 at 21:27

1 Answer 1

1

Try this:

subs_filter '(<link.*?href=(.))[^"']+\.css([^"']+\2[^>]*?>)' '$1new.css$3' irg;

DESCRIPTION

Regular expression visualization

DEMO

https://regex101.com/r/aF2aU9/1

Sign up to request clarification or add additional context in comments.

1 Comment

I'm no longer working on this but will give it a shot soon when I have the opportunity.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.