1

I have the following CSS background-image rules wherein I want the URL replaced with a new domain (you know, for the sake of loading images via a cookie-less domain):

  1. background-image : url(/image/test.png);
  2. background-image : url("/image/test.png");
  3. background-image : url('/image/test.png');

I can catch the quotes before /image and then replace the URL correctly with the following sed expression:

local someVar=123
local replace='url\("\/\/img.mydomain.com\/'"$someVar"'\1\")'
sed -E 's,url\(.(.+).\),'"$replace"',g' file.css

The snippet

url\(.

takes care of the quote (single or double) in the URL string. But if the quotes are not there (first example above), the regex will obviously fail. What would be the correct regex to check for this?

I tried the following but it doesn't work:

local someVar=123
local replace='url\("\/\/img.mydomain.com\/'"$someVar"'\1\")'
sed -E 's,url\((\'|\")(.+)(\'|\")\),'"$replace"',g' file.css

and it just throws error: syntax error near unexpected token `)'

3 Answers 3

3

You can use this sed:

local someVar=123
local replace='\1\2//img.mydomain.com/'"$someVar"'\3\4'

On Linux Use this sed command:

sed -r "s~(url\()(['\"]?)(.*)$2(\);)~$replace~" file.css

On OSX Use this sed command:

sed -E "s~(url\()(['\"]?)(.*)$2(\);)~$replace~" file.css

OUTPUT:

background-image : url(//img.mydomain.com/123/image/test.png);
background-image : url("//img.mydomain.com/123/image/test.png");
background-image : url('//img.mydomain.com/123/image/test.png');
Sign up to request clarification or add additional context in comments.

6 Comments

If I use the "-r" option, I get an "illegal option" error. Without the "-r", I get: "s~(url()(['"]?)(.*?)(\ ...": \2 not defined in the RE
Is it OSX that you are using?
So sorry, I should have asked earlier. Just updated the answer with OSX sed command as well.
Thanks Anubhava. This time it's a different error: "s~(url()(['"]?)(.*?)(\ ...": RE error: repetition-operator operand invalid
No I had removed .*? command is this: sed -E "s~(url\()(['\"]?)(.*)$2(\);)~$replace~" file.css
|
2

According to this answer (How to match a single quote in sed), you should use another pair of single quote:

local someVar=123
local replace='url\("\/\/img.mydomain.com\/'"$someVar"'\1\")'
sed -E 's,url\(('\''|\")(.+)('\''|\")\),'"$replace"',g' file.css

For better readability, you can use square brackets instead of the parentheses, and you can just put the double quote without escaping it.

sed -E 's,url\(['\''"]?([^'\'']+)['\''"]?\),'"$replace"',g' file.css

3 Comments

How do I correctly match "background-image : url(/image/test.png);" where the quotes are missing?
Cool! That worked fine. But when I use the above in the following case: url('/images/test.png'), it gives me an output that reads as: url("//img.mydomain.com/123/images/test.png' "); There's a trailing ' after "png"
Hmm, we must change the (.+) to not match the trailing quote. See my edits
1
someVar=123
replace='\/\/img.mydomain.com\/'"$someVar"''
sed  -E 's,(\/.*\/[a-z.]+),'"$replace"'\1,g' file.css

Output:

background-image : url(//img.mydomain.com/image/test.png);
background-image : url("//img.mydomain.com/image/test.png");
background-image : url('//img.mydomain.com/image/test.png');

Comments

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.