0

I'm asyncronously loading a css.

@font-face{
    src: url('fonts/le-havre-1_300_normal-webfont.eot');
    src: url('fonts/le-havre-1_300_normal-webfont.eot?#iefix') format('embedded-opentype'),
    url('fonts/le-havre-1_300_normal-webfont.woff') format('woff'),
    url('fonts/le-havre-1_300_normal-webfont.ttf') format('truetype'),
    url('fonts/le-havre-1_300_normal-webfont.svg#LeHavreLight') format('svg');
}

I have to replace the path dynamically, for example:

url(' -> url('http://www.site.com/skin/light/

I wrote this regular expression but it seems to replace only the first value.

var newCss = cssText.replace(/url\(\'(.+)\'\)/g, function(a,b){
    return 'url(\''+'http://www.site.com/skin/light/' + b + '\')';
});

How can I replace all the url attributes?

2
  • perfect! but why do you write the answer in a comment? Commented Jul 26, 2011 at 18:27
  • couldn't remember if multi-line was disabled in this case. Commented Jul 26, 2011 at 18:36

2 Answers 2

1

Try the /m modifier too. Since the string has multiple lines, it probably needs it.

/url\('([^)]+)'\)/gm
Sign up to request clarification or add additional context in comments.

Comments

0

The regexp/url\(\'(.+?)\'\)/g solves the problem, because of the un-greediness of the expression .+?, in contrast to the greedy expression .+ that matches to the entire following string
(if multi-line enabled):

url('fonts/le-havre-1_300_normal-webfont.eot');
src: url('fonts/le-havre-1_300_normal-webfont.eot?#iefix') format('embedded-opentype'),
url('fonts/le-havre-1_300_normal-webfont.woff') format('woff'),
url('fonts/le-havre-1_300_normal-webfont.ttf') format('truetype'),
url('fonts/le-havre-1_300_normal-webfont.svg#LeHavreLight') format('svg')

See the "U (PCRE_UNGREEDY)" modifier: Pattern Modifiers @ php.net

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.