后有一个字符串
<!--
document.write("<a rel='nofollow' href='mailto:tranquangdieu2704@gmail.com'>tranquangdieu2704@gmail.com</a>");
//-->
如何在标签中获取htmlentities
tranquangdieu2704@gmail.com
如果您想获得每个html实体:
const rgx = /&#d+;/g;
const string = "<a rel='nofollow' href='mailto:tranquangdieu2704@gmail.com'>tranquangdieu2704@gmail.com</a>";
while ((match = rgx.exec(string)) !== null) console.log(match[0]);
如果你想把它们都放在一起:
const rgx = /(&#d+;)+/;
const string = "<a rel='nofollow' href='mailto:tranquangdieu2704@gmail.com'>tranquangdieu2704@gmail.com</a>";
console.log(rgx.exec(string)[0]);
这种RegEx的优点是可以处理包含HTMLEntities的每个字符串,而不考虑周围环境。
const html = "<a rel='nofollow' href='mailto:tranquangdieu2704@gmail.com'>tranquangdieu2704@gmail.com</a>";
const match = /<a[^>]+>([^<]+)</a>/.exec(html);
console.log('match: ', match[1]);
console.log('is-correct: ', match[1] === 'tranquangdieu2704@gmail.com');
这是有效的,但我不明白为什么在标记中会有那么多实体编码。
只需使用split("")
即可实现。我认为这是一个更好的解决方案,因为它不在乎href
内部有什么,所以它可以是任何字符串,它会将其拆分。
const a = `document.write("<a rel='nofollow' href='mailto:tranquangdieu2704@gmail.com'>tranquangdieu2704@gmail.com</a>");`
const array = a.split("mailto:")[1].split("</a>");")[0]
console.log(array)
试试这个正则表达式:
const matches = str.match(/&#d+;/);