什么正则表达式匹配所有出现的css网址? [英] What regex to match all occurrences of css urls?
问题描述
我有一个包含一堆背景图片网址的CSS文件:
.alert-01 {
background :url('img / alert-01.jpg')center no-repeat;中心无重复;
}
.alert-02 {
background:url('img / alert-02.jpg'
}
.alert-03 {
background:url('img / alert-03.jpg')center no-repeat;
}
.alert-04 {
background:url('img / alert-04.jpg')center no-repeat;
}
我想写一个正则表达式,
所以最初我会得到:
url('img /
url('img / alert-02.jpg')
url('img / alert-03.jpg')
url('img / alert- 04.jpg')
这样我就可以替换位来将它们转换成html < img>
阻止。
我最后一点罚分,但我的正则表达式的技能有点生锈。 >
我最近的尝试是: / ^ url\(。*?g\)$ / ig
任何帮助?
BTW我在javascript中做这一切,但是语言不应该真正影响正则表达式。
提前感谢!
此外,只是为了注意,我不能保证文件总是是jpg,可能是jpeg,png等。而且,到背景的参数总是相同的。
$ b
/ url\(。*?g'\)/ ig
b $ b
您的错误是在regexp中包含 ^
和 $
结束输入字符串。你忘了允许报价。
i have a css file with a bunch of background image urls:
.alert-01 {
background: url('img/alert-01.jpg') center no-repeat;
}
.alert-02 {
background: url('img/alert-02.jpg') center no-repeat;
}
.alert-03 {
background: url('img/alert-03.jpg') center no-repeat;
}
.alert-04 {
background: url('img/alert-04.jpg') center no-repeat;
}
And I'd like to write a regex that would strip out the urls.
So initially i would get:
url('img/alert-01.jpg')
url('img/alert-02.jpg')
url('img/alert-03.jpg')
url('img/alert-04.jpg')
Which i could then replace bits to turn them into html <img>
blocks.
I'm fine with the last bit, but my regex skills are a bit rusty.
My latest try was this: /^url\(.*?g\)$/ig
Any help?
BTW I was doing this all in javascript, but the language shouldn't affect the regex really.
Thanks in advance!
Also, just to note, i can't guarantee that the files will always be jpg and may be jpeg, png etc. Also that the arguments into background will always be the same. Hence why i want to extract just the url part.
Try:
/url\(.*?g'\)/ig
Your mistake was including ^
and $
in the regexp, which anchors it to the beginning and end of the input string. And you forgot to allow for the quote.
这篇关于什么正则表达式匹配所有出现的css网址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!