0

String

url(image1.png), url('image2.png'), url("image3.png")

Note the ' and " delimiters. It would be nice to handle them as well, but I'm happy if the first form is captured.

Result

var results = 'image1.png', 'image2.png', 'image3.png';

How?

I would like to use regular expressions in javascript to parse a string that is used as the CSS property of an element with multiple backgrounds. The aim is to get the url of the images that are put in the background of the element using CSS.

See more: http://www.css3.info/preview/multiple-backgrounds/

JSFiddle

http://jsfiddle.net/fcx9x59r/1/

4 Answers 4

2

For this particular string [^'"()]+(?=['")]) seems to work fine:

css = "url(image1.png), url('image2.png'), url(\"image3.png\")";

m = css.match(/[^'"()]+(?=['")])/g)
document.write(m)

In the general case, you have to resort to a loop, because JS doesn't provide a way to return all matching groups from a single call:

css = "url(image1.png), something else, url('image2.png'), url(\"image3.png\")";

urls = []
css.replace(/url\(['"]*([^'")]+)/g, function(_, $1) { urls.push($1) });

document.write(urls)

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

4 Comments

I thought String.prototype.match exactly returns all matches (if the regexp has the g option).
It works but I didn't understand that (?=[ part. Can you explain a little more? Thanks.
@AlexStack is this the regex you want? It won't work in many cases. It would also match the string foo in "foo)
@AlexStack: ?= is a lookahead. Basically, it reads "match ... when followed by...".
1
/url\(([^"']+?)\)|'([^'"]+?)'\)|"([^'"]+?)"\)/g

Try this.Grab the captures.See demo.

http://regex101.com/r/qQ3kG7/1

2 Comments

I like your solution for its brevity and the use of Regex101 website. However this parses the following string url(image1.png), url('image2.png"), url('image3.png") too. Depends on how you look at it this can be a bug or a feature. Can we fix that?
Yes this last one is the best. Can you put that in your answer above?
1

You could try the below code also.

> var s = 'url(image1.png), url(\'image2.png\'), url("image3.png")';
undefined
> var re = /url\((["'])?((?:(?!\1).|[^'"])*?)\1\)/g;
undefined
> var matches = [];
undefined
> var m;
undefined
> while ((m = re.exec(s)) != null) {
......... matches.push(m[2]);
......... }
3
> console.log(matches)
[ 'image1.png', 'image2.png', 'image3.png' ]

DEMO

3 Comments

would be great to do all of it in just one regexp. (no while loop)
Why do you need to do it in one regexp?
Because it's smaller and probably faster.
1

Try this:

var results = str.match(/url\(['"]?[^\s'")]+['"]?\)/g)
.map(function(s) {
    return s.match(/url\(['"]?([^\s'")]+)['"]?\)/)[1];
});

Demo

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.