我正在异步加载 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');
}
我必须动态替换路径,例如:
url(' -> url('http://www.site.com/skin/light/
我写了这个正则表达式,但它似乎只替换了第一个值。
var newCss = cssText.replace(/url\(\'(.+)\'\)/g, function(a,b){
return 'url(\''+'http://www.site.com/skin/light/' + b + '\')';
});
如何替换所有 url 属性?
最佳答案
也试试/m 修饰符。由于字符串有多行,它可能需要它。
/url\('([^)]+)'\)/gm
关于javascript - 在异步加载的 css 上用正则表达式替换相对路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6834622/