[英]regular expression to extract a link tag from an html string on server side
I have an HTML page source which is in string format on the server-side 我有一个HTML页面源,它在服务器端是字符串格式的
I need to extract a from the string and add it to an array.There can be multiple links with the same starting tag. 我需要从字符串中提取一个并将其添加到数组中。可以有多个具有相同开始标记的链接。 i need to push the extracted string to an array
我需要将提取的字符串推送到数组
the <link rel="icons"................ >
can contain anything inside the tag.I have mentioned the startTag and endTag in the code below. <link rel="icons"................ >
可以在标记内包含任何内容。我在下面的代码中提到了startTag和endTag。
var startTag = '<link rel="icons"';
var endTag = '>';
const re = new RegExp('(' + startTag + ')(.|\n)+?(' + endTag + ')', 'g');
However, When i console the value of re, it is not the one I expect. 但是,当我管理re的值时,它不是我期望的值。
DesiredOutput DesiredOutput
['<link rel="icons" href="icons1.png"','<link rel="icons" href="icons2.png"',<link rel="icons" href="icons3.png"]
Thanks in advance. 提前致谢。
I think you're looking for something like this (the replace is just to remove extra whitespace): 我认为您正在寻找类似的东西(替换只是为了删除多余的空格):
const data = ` <link rel="icons" href="icons1.png" > <link rel="icons" href="icons2.png" > <link rel="icons" href="icons3.png" > `; const links = data.match(/<link.*?>/gs) .map(link => link.replace(/\\s+/g, ' ')); console.log(links);
If you're in an environment that doesn't support the s
flag, you could use /<link[^]*?>/g
instead. 如果您所处的环境不支持
s
标志,则可以改用/<link[^]*?>/g
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.