js字符窜转换成数组

<img alt="image" src="http://www.qqbsmall.com/data/...; /><img alt="image" src="http://www.qqbsmall.com/data/...; /><img alt="image" src="http://www.qqbsmall.com/data/...; /><img alt="image" src="http://www.qqbsmall.com/data/...; /><img alt="image" src="http://www.qqbsmall.com/data/...; /><img alt="image" src="http://www.qqbsmall.com/data/...; /><img alt="image" src="http://www.qqbsmall.com/data/...; /><img alt="image" src="http://www.qqbsmall.com/data/...; /><img alt="image" src="http://www.qqbsmall.com/data/...; />

这一串字符串怎么把src中的地址切出来 转换成数组

阅读 4.1k
7 个回答
        var data = '<img alt="image" src="http://www.qqbsmall.com/data/...;" /><img alt="image" src="http://www.qqbsmall.com/data/...;" /><img alt="image" src="http://www.qqbsmall.com/data/...;" /><img alt="image" src="http://www.qqbsmall.com/data/...;" /><img alt="image" src="http://www.qqbsmall.com/data/...;" /><img alt="image" src="http://www.qqbsmall.com/data/...;" /><img alt="image" src="http://www.qqbsmall.com/data/...;" /><img alt="image" src="http://www.qqbsmall.com/data/...;" /><img alt="image" src="http://www.qqbsmall.com/data/...;" />';
        var array = data.match(/(?<=src=("|'))[^"']+/g);
        console.log(array)

正则很有意思 建议了解一下

var str = '<img alt="image" src="http://www.qqbsmall.com/data/..."; /><img alt="image" src="http://www.qqbsmall.com/data/..."; /><img alt="image" src="http://www.qqbsmall.com/data/..."; /><img alt="image" src="http://www.qqbsmall.com/data/..."; /><img alt="image" src="http://www.qqbsmall.com/data/..."; /><img alt="image" src="http://www.qqbsmall.com/data/..."; /><img alt="image" src="http://www.qqbsmall.com/data/..."; /><img alt="image" src="http://www.qqbsmall.com/data/..."; /><img alt="image" src="http://www.qqbsmall.com/data/..."; />';

var strArray = str.split('><')

var result = strArray.map(function(str) {
    return str.replace(/<*img.+src\="(.+)".*/ig, '$1'); 
});

console.log(result);

思路:先把每个img分开,截取每个img的src="和"/>之间的字符串,push进新数组

例如

<img id="img1" />
document.getElementsById('img1').src

这样就获取到了路劲,在通过字符串截取自己想要的部分,之后push进去

let imgArray = [];
imgArray.push({
 document.getElementsById('img1').src
}) 

就完成了

抖个机灵:

let str = `<img alt="image" src="http://www.qqbsmall.com/data/..." /><img alt="image" src="http://www.qqbsmall.com/data/..." /><img alt="image" src="http://www.qqbsmall.com/data/..." /><img alt="image" src="http://www.qqbsmall.com/data/..." /><img alt="image" src="http://www.qqbsmall.com/data/..." /><img alt="image" src="http://www.qqbsmall.com/data/..." /><img alt="image" src="http://www.qqbsmall.com/data/..." /><img alt="image" src="http://www.qqbsmall.com/data/..." /><img alt="image" src="http://www.qqbsmall.com/data/..." />`;
let div = document.createElement('div');
div.innerHTML = str;
let src = Array.from(div.children).map(v => v.src);
console.log(src);

不如楼上,不如正则,不抖了

我也来一个~~

var str = '<img alt="image" src="http://www.qqbsmall.com/data/..."; /><img alt="image" src="http://www.qqbsmall.com/data/..."; /><img alt="image" src="http://www.qqbsmall.com/data/..."; /><img alt="image" src="http://www.qqbsmall.com/data/..."; /><img alt="image" src="http://www.qqbsmall.com/data/..."; /><img alt="image" src="http://www.qqbsmall.com/data/..."; /><img alt="image" src="http://www.qqbsmall.com/data/..."; /><img alt="image" src="http://www.qqbsmall.com/data/..."; /><img alt="image" src="http://www.qqbsmall.com/data/..."; />';


console.log(str.match(/(?<=src=")(\S*)(?=")/g));
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题