我需要使用 JavaScript 从网页源中搜索并找到确切的图像 URL。
目前我只能统计出现的次数。
我的代码是:
var count = 0;
var pos = res.body.indexOf("650x365");
while(pos > -1){
++count;
pos = res.body.indexOf("650x365", ++pos);
}
console.log(count); // 2
网址是这样的
http://img.sa.com/c/1/70/650x365/bp/02112015_esa.jpg
最佳答案
使用某种正则表达式,下面是一个对 url 做出一些假设的示例:
var urls = res.body.match(/http:\/\/img\.sa\.com\/c\/\d+\/\d+\/650x365\/bp\/.+?\.jpg/g);
关于javascript 查找字符串中随机出现的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44058581/