I’m running the following code on Webkit:
var scriptElements = document.scripts;
var scriptUrls = [];
// URL matching
var regexp = /\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))/i;
for (var i = 0; i < scriptElements.length; i++) {
element = scriptElements[i];
var urls = element.innerHTML.match(regexp);
console.log('local', urls);
scriptUrls.concat(urls);
console.log('global', scriptUrls);
}
I see non-empty arrays printed after ‘local’ but the ‘global’ always stays as an empty array. What’s going on?
.concatcreates a new Array. You need to overwrite the old one.Or if you want to keep the original
scriptUrlsArray, you can.push()the values in.This uses
.apply()to converturlsinto individual arguments passed to.push(). This way the content ofurlsis added toscriptUrlsas individual items.Also, note that
.concat()flattens the Array. If you wanted an Array of Arrays, then you’d usescriptUrls.push(urls).