0

I am having trouble matching a specific pattern in a backgroundImage css expression. I want to get the u/user/rest/of/stuff out of "url(foo/bar/u/user/rest/of/stuff)". However I am not sure how to get only the second u (the u in url would be the first), plus everything after that up to the second parenthesis.

1
  • 1
    Obtain the url() value first, then isolate the string inside it and work with that. Commented Aug 19, 2014 at 16:45

2 Answers 2

1

Use word boundary(\b) ,

> "url(foo/bar/u/user/rest/of/stuff)".match(/\bu\b[^)]*/g);
[ 'u/user/rest/of/stuff' ]

OR

Use a look-ahead (?=...)

> "url(foo/bar/u/user/rest/of/stuff)".match(/u[^()]*(?=\))/g);
[ 'u/user/rest/of/stuff' ]
Sign up to request clarification or add additional context in comments.

Comments

0

For I produce a lot of typos in regex and for better readability I use functions whenever possible for this kind of stuff...

function GetSubstring(str, from, to) {
    return str.substring(str.lastIndexOf(from)+from.length,str.lastIndexOf(to));
}

And then use

GetSubstring('url(foo/bar/u/user/rest/of/stuff)','url(foo/bar/',')');

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.