这让我感到困惑,尽管我认为问题很简单。
鉴于这段代码:
var text = ' url("https://") repeat scroll 0% 0%, url("https://") repeat scroll 0% 0%, url("https://") repeat scroll 0% 0% red';
var result = /(url)/.exec(text);
我得到了["url", "url"]
..我期望的结果["url", "url", "url"]
任何解释为什么它只捕获 2 个 url 实例而不是 3 个
谢谢
更新接受的答案
实际上这是我为 HeadJS 重写的 Modernizr 函数的一部分,所以最终函数将是:
function multiplebgs() {
var ele = document.createElement('div');
var style = ele.style;
style.cssText = 'background:url(https://),url(https://),red url(https://)';
// If the UA supports multiple backgrounds, there should be three occurrences
// of the string "url(" in the return value for elemStyle.background
var result = (style.background || "").match(/url/g);
return Object.prototype.toString.call( result ) === '[object Array]' && result.length === 3;
}
console.log(multiplebgs());
现在将在受支持的浏览器上正确返回 true 。
如果有人看到这方面的其他问题,请发表评论,谢谢!