2

I have regular expression

/url\\(\\s*(?!data:)([^\\)\\s]+)\\s*\\)?/

It searches in css for lines like this

background-image: url(/path.to/img.png);

And ignores the lines

background-image: url(data:image/svg+xml;base64, PHN2ZyB4bWxucz) no-repeat;

It is used to find image paths and replace them.

But if Data url has quotes

background-image: url("data:image/svg+xml;base64, PHN2ZyB4bWxucz") no-repeat;

Regular expression matches it. I need to match usual image url and ignore Data url with single, double quotes or without them.

4
  • That's nice. Good luck figuring that out. Did you have a question? Commented Feb 14, 2014 at 9:26
  • That is actually my question because I am not strong in regular expressions and will spend days to do that. Maybe someone do that faster then me. Commented Feb 14, 2014 at 9:30
  • Questions usually have a ? in them... all you've done is treat this place as a "do my job for me" jobs board. Commented Feb 14, 2014 at 9:31
  • 1
    But others also asked such questions and I answered them if I knew. Commented Feb 14, 2014 at 9:33

2 Answers 2

2
url\\(\\s*(?!["']?data:)([^)]+)\\)

In this regex, it will ignore anything like below(accepting optional space after (

url("data...
url(data:...

Which means it will only match the first example that you have given on the question.

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

3 Comments

Thanks for the reply but your expression does not match background-image: url("/path/to/img.jpg");.
Thanks your answer do exactly what I want.
Thanks. I have added checking for an eventual space before data or "dataor 'data: url\(\\s*(?!`\\s*["']?data:)([^)]+)\)
1

Try to use pattern with ("|')?:

/url\\(("|')?\\s*([^(:;,]+)\\s*("|')?\\);?/

("|')? allow you also to match single or double quotes

3 Comments

Sorry I tried /url\\((\"|')?\\s*(?!data:)([^\\)\\s]+)\\s*(\"|')?\\)?/ but it still matches data url with both quotes(.
Now its not matching string with usual url. I think ("|')? should be used somewhere inside (?!data:).
Using :;, instead of data is cool idea. But as a result it is not selecting sub strings as I expected. The another answer suits me perfect. I like your answer but cannot accept it.

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.