[英]How to get content inside html tags including the tags using regex in javascript?
我在下面有文字 -
how much production in batu
现在此文本显示为一系列html
标签。 基本上,每个单词都包含在具有特定style
或class
的span
中。 这是它的样子
'<span style="">how </span><span style="">much </span><span class="pink-highlight">production </span><span style="">in </span><span class="yellow-highlight">batu</span>'
现在我想从这个html
字符串中得到两件事:样式或 class 和跨度内的内容(没有
)
所以我想要一个字符串中的以下信息array
[["", "how"], ["", "much"], ["pink-highlight", "production"], ["", "in"], ["yellow-highlight", "batu"]]
现在这可以使用regex
轻松完成。 但我并不精通regex
。 我能想到的模式
<span>(.*?)</span>
但它只会找出span
内的内容,甚至在这种情况下都不起作用,因为每个span
都有一个style
标签或一个 class。
那么在这种情况下,什么regex
最适合获得所需的结果呢?
使用 reg exp 可能会因匹配 HTML 而失败。 将其解析为 HTML 并获取数据非常容易。
var html = '<span style="">how </span><span style="">much </span><span class="pink-highlight">production </span><span style="">in </span><span class="yellow-highlight">batu</span>' var temp = document.createElement('div') temp.innerHTML = html var data = Array.from(temp.querySelectorAll('span')).map(span => ([ span.getAttribute("style") || span.getAttribute("class") || '', span.textContent.trim() ]) ) console.log(data)
我将提供一个简单的正则表达式。 实际上,我只是添加了另外 2 个选项。
(<span>(.*?)<\/span>)|(<span style=".*?">(.*?)<\/span>)|(<span class=".*?">(.*?)<\/span>)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.