比如我有一个变量content,里面是字符串:<p><a href="http://www.baidu.com" target="_self"><img src="/wzomifiles/ftpfile/pic/2016/09/30/84e228e30f8d404ea7972eee988567e4.JPEG" title="84e228e30f8d404ea7972eee988567e4.JPEG"/></a></p><p><a href="http://www.tengxun.com" target="_self"><img src="/wzomifiles/ftpfile/pic/2016/09/30/a5acfc450c334584aee43e720c8274c1.JPEG" title="a5acfc450c334584aee43e720c8274c1.JPEG"/></a></p>我想吧每个p标签(段落)里的href值,src值提取出来变成一个对象,如果有多个P标签,就把提取出的内容变成个对象数组,比如变成这样:[{href:"http://www.baidu.com",src:"/wzomifiles/ftpfile/pic/2016/09/30/84e228e30f8d404ea7972eee988567e4.JPEG"},{href:"http://www.tengxun.com",src:"/wzomifiles/ftpfile/pic/2016/09/30/84e228e30f8d404ea7972eee988567e1.JPEG"}]我该怎么写方法呢?新手求教,还没学习正则,规则看的有点晕,实在写不出来,能否给出以上需求的实例代码,非常感谢!
1 回答

噜噜哒
TA贡献1784条经验 获得超7个赞
function contentParse(str) {
var sr = /<\/p><p>/;
var arr = str.split(sr);
var r = [];
for (var i = 0; i < arr.length; i++) {
r.push({
href: arr[i].match(/href="(.+?)"/)[1],
src: arr[i].match(/src="(.+?)"/)[1]
});
}
return r;
}
添加回答
举报
0/150
提交
取消