我想匹配CSS中的所有外部资源。例如,content包含2个资源,例如//cdn.com/roboto-regular.eot//cdn.com/roboto-bold.eot

var reg = /\/\/.*\.(js|css|jpg|eot)[^\/\/]/ig;

var content="url(//cdn.com/roboto-regular.eot);src:url(//cdn.com/roboto-bold.eot#iefix)";

while ((match = reg.exec(content)) != null) console.log('match', match);


但是表达式结果为"//cdn.com/roboto-regular.eot);src:url(//cdn.com/roboto-bold.eot#"

我怎样才能使表达式不贪心,这样我才能得到每一个匹配项?

最佳答案

您可以从匹配字符列表中排除一些字符:



var reg = /\/\/[^)(]*\.(js|css|jpg|eot)/ig;
var content="url(//cdn.com/roboto-regular.eot);src:url(//cdn.com/roboto-bold.eot#iefix)";
while ((match = reg.exec(content)) != null) console.log('match', match);





[^)(]表示everything except ")" and "("

关于javascript - JavaScript的非贪婪正则表达式,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47049062/

10-10 22:22