3

I am trying to construct a REGEX pattern, to convert relative URLs to absolute URLs in javascript scripts.

Example: I would like to replace ALL instances of the following (taken from js script):

url('fonts/fontawesome-webfont.eot?v=4.2.0');

And return the following:

url('http://example.com/fonts/fontawesome-webfont.eot?v=4.2.0');

Example pattern that works for HTML tags (link):

$pattern = "#(<\s*a\s+[^>]*href\s*=\s*[\"'])(?!http)([^\"'>]+)([\"'>]+)#"

Preferred use of this pattern:

$result = preg_replace($pattern,'$1http://example.com/$2$3', $result);

The closest I have managed to guess (unsuccessfully) is:

$pattern = "#(url\s*=\s*[\"'])(?!http)([\"'])#";
1
  • You don't have an =s in your search string, or not in the form url =.. Try (url\(["'])(?!http). Commented Jun 16, 2016 at 19:21

3 Answers 3

2

Something like this might work for you:

/(?<=url\((['"]))(?!http)(?=.*?\1)/

With replacement:

http://example.com/

Regex Demo

The above regex will match the position just after url(' where the quote can also be a double quote.

(?<=...) # is a positive lookbehind
(?!...)  # is a negative lookahead
(?=...)  # is a positive lookahead
\1       # refers to capturing group 1, in this case either ' or "

Note that lookbehinds isn't supported in JavaScript but are in PHP.

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

2 Comments

Thanks, this is a great help!
@LeenRemmelzwaal I hope I have given enough information on how this regex works, you can consider looking up lookarounds on google :-)
0

My testing shows that this will work: /url\\(['\"](?!http[s]?:\\/\\/)(.+)['|\"]\\)/

So your replace should look like:

preg_replace("/url\\(['\"](?!http[s]?:\\/\\/)(.+)['|\"]\\)/",'http://example.com/$1', $result)

Not a regex master tho - so take this with a grain of salt

1 Comment

A character class is a list of optional characters. So the s doesn't need to be in one and the ['|\"] doesn't need the or the or actual makes the pipe optional as well. You also could capture the first quote type and use a back reference for the second to make sure you match. e.g. regex101.com/r/sY5sN2/1 (if using single quotes for the regex encapsulation, if double the ``s need to be doubled)
0

The solution I've used (thanks to help from andlrc and chris85):

$result = preg_replace("#(?<=url\((['\"]))(?!https?)#",'http://example.com/', $result);

4 Comments

The [s] is requiring an s did you mean to make the s optional?
Yes please. How do you recommend I make the 's' optional?
Use s? the question mark will make s match zero or one time
Like this https?. Also your second and third capture groups are empty, replacing with http://example.com/ will yield the same result. eval.in/590456 The ? makes the previous character, or group, optional.

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.