简体   繁体   English

正则表达式转换成元素

[英]regex into elements into array

I'm using /<img\\s+[^>]*src="([^"]*)"[^>]*>/g to search HTML for <img> tags. It is working. However, its passing into an array as 1 element when there are multiple tags. Is there a way to pass EACH tag into the array as a unique element? Ie 我正在使用/<img\\s+[^>]*src="([^"]*)"[^>]*>/g在HTML中搜索<img>标签。它正在工作,但是,它的传递当有多个标签时,将数组作为1个元素放入数组中,是否有一种方法可以将EACH标签作为唯一元素传递到数组中?

var arr = [img TAG1, img TAG2, img TAG3 ,img TAG4] var arr = [img TAG1,img TAG2,img TAG3,img TAG4]

For now it does this and looks like each tag is it's own element but it's not. 现在,它执行此操作,并且看起来每个标签都是它自己的元素,但不是。 Here is an output. 这是输出。

var list = arr[0];

Output of script: 脚本输出:

list =
    <'img' src="/RightAnswers/WB FAQs/Mail &amp; Collaboration/090101028112912.jpg" alt="Inline Image" />,
    <'img' src="/RightAnswers/WB FAQs/Mail &amp; Collaboration/090101028113127.jpg" alt="Inline Image" />,
    <'img' src="/RightAnswers/WB FAQs/Mail &amp; Collaboration/090101028113955.jpg" alt="Inline Image" />,
    <'img 'src="/RightAnswers/WB FAQs/Mail &amp; Collaboration/090101028114034.jpg" alt="Inline Image" />
    <'img' src="/RightAnswers/WB FAQs/Mail &amp; Collaboration/090101028114105.jpg" alt="Inline Image" />,
    <'img' src="/RightAnswers/WB FAQs/Mail &amp; Collaboration/090101028114345.jpg" alt="Inline Image" />

Note- I had to wrap the <img> tags in a quote ('img') to submit this question. 注意-我必须将<img>标记括在引号('img')中才能提交此问题。 They are NOT present in the actual output. 它们不在实际输出中。

var list = arr[1];

Output of script: 脚本输出:

list=undefined

I have seen that regex is not the best tool for parsing HTML, I need to do this once as part of an import into ServiceNow so I don't really have access to parsing tools. 我已经看到,正则表达式不是解析HTML的最佳工具,在导入ServiceNow的过程中,我需要执行一次此操作,因此我实际上没有访问解析工具的权限。 Unless there is something I can run a CSV through to grab JUST the file name from the <img> tags. 除非有什么办法,否则我可以运行CSV来仅从<img>标记中获取文件名。

Why not use javascript to collect all img elements instead of regex? 为什么不使用javascript而不是正则表达式来收集所有img元素?

Like this: 像这样:

 var imgCollection = document.getElementsByTagName('img'); console.log(imgCollection); 
 <div id="just-a-div"><p>random paragraph</p></div> <img src="/RightAnswers/WB FAQs/Mail &amp; Collaboration/090101028112912.jpg" alt="Inline Image" /> <img src="/RightAnswers/WB FAQs/Mail &amp; Collaboration/090101028113127.jpg" alt="Inline Image" /> <img src="/RightAnswers/WB FAQs/Mail &amp; Collaboration/090101028113955.jpg" alt="Inline Image" /> <div></div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM