将html编码的字符串转换为数组

时间:2016-11-17 19:14:41

标签: javascript html

我有这个api调用,返回一个字符串,包含一大块图像。如何将该字符串转换为数组?

返回的字符串如下所示。从

标签开始,因为它是一个wordpress帖子。

它的所有图像标签src都在数组中。

<p><img src=\"http:\/\/local.pp.com\/api\/wp-content\/uploads\/2016\/10\/thumb-12.jpg\" alt=\"thumb\" width=\"720\" height=\"510\" class=\"alignnone size-full wp-image-52\" srcset=\"http:\/\/local.pp.com\/api\/wp-content\/uploads\/2016\/10\/thumb-12.jpg 720w, http:\/\/local.pp.com\/api\/wp-content\/uploads\/2016\/10\/thumb-12-300x213.jpg 300w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><br \/>\n<img src=\"http:\/\/local.pp.com\/api\/wp-content\/uploads\/2016\/10\/thumb-12.jpg\" alt=\"thumb\" width=\"720\" height=\"510\" class=\"alignnone size-full wp-image-52\" srcset=\"http:\/\/local.pp.com\/api\/wp-content\/uploads\/2016\/10\/thumb-12.jpg 720w, http:\/\/local.pp.com\/api\/wp-content\/uploads\/2016\/10\/thumb-12-300x213.jpg 300w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><br \/>\n<img src=\"http:\/\/local.pp.com\/api\/wp-content\/uploads\/2016\/10\/thumb-12.jpg\" alt=\"thumb\" width=\"720\" height=\"510\" class=\"alignnone size-full wp-image-52\" srcset=\"http:\/\/local.pp.com\/api\/wp-content\/uploads\/2016\/10\/thumb-12.jpg 720w, http:\/\/local.pp.com\/api\/wp-content\/uploads\/2016\/10\/thumb-12-300x213.jpg 300w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><br \/>\n<img src=\"http:\/\/local.pp.com\/api\/wp-content\/uploads\/2016\/10\/thumb-12.jpg\" alt=\"thumb\" width=\"720\" height=\"510\" class=\"alignnone size-full wp-image-52\" srcset=\"http:\/\/local.pp.com\/api\/wp-content\/uploads\/2016\/10\/thumb-12.jpg 720w, http:\/\/local.pp.com\/api\/wp-content\/uploads\/2016\/10\/thumb-12-300x213.jpg 300w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><br \/>\n<img src=\"http:\/\/local.pp.com\/api\/wp-content\/uploads\/2016\/10\/thumb-12.jpg\" alt=\"thumb\" width=\"720\" height=\"510\" class=\"alignnone size-full wp-image-52\" srcset=\"http:\/\/local.pp.com\/api\/wp-content\/uploads\/2016\/10\/thumb-12.jpg 720w, http:\/\/local.pp.com\/api\/wp-content\/uploads\/2016\/10\/thumb-12-300x213.jpg 300w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n"

2 个答案:

答案 0 :(得分:3)

sys.path.append('YOUR PATH TO CAFFE')

创建一个div,将html放入其中,然后使用getElementsByTagName来获取图像。

getElementsByTagName返回一个HTMLCollection,它就像一个数组,可以通过for循环以相同的方式迭代。

var div = document.createElement('div');
div.innerHTML = myHTMLString;
var images = div.getElementsByTagName('img');

编辑:你说你想要数组中的src&#39。看看这个:https://jsfiddle.net/kev7hr2y/

答案 1 :(得分:0)

您可以使用DOMParser将字符串解析为DOM元素,然后找到所有<img>map()src属性:

var parser = new DOMParser(),
    parsedElms = parser.parseFromString(yourHTMLString, 'text/html'),
    imgs = parsedElms.getElementsByTagName('img');

var result = Array.prototype.map.call(imgs, function(e) { return e.getAttribute('src'); });