我正在编写一个代码来从 HTML 代码的标签中提取所有纯内容。我知道这可以使用文档元素来完成。但我需要只使用 REGEX 来做到这一点 我编写了以下代码,但它有一些错误,我无法弄清楚如何解决它。
function htmlToText(html) {
return html.
replace(/(.|\n)*<body.*>/, ''). //remove up till body
replace(/<\/body(.|\n)*/, ''). //remove from </body
replace(/<.+\>/, ''). //remove tags
replace(/^\s\n*$/gm, ''); //remove empty lines
}
解决办法如下
function htmlToText(html) {
return html.
replace(/(.|\n)*<body.*>/, ''). //remove up till body
replace(/<\/body(.|\n)*/g, ''). //remove from </body
replace(/<.+\>/g, ''). //remove tags
replace(/^\s\n*$/gm, ''); //remove empty lines
}
最佳答案
不用想太多,document.body.innerText
就可以了
A Sample Document
Some strong and emphasized text
关于javascript - 使用 REGEX 在 Node js 中提取 HTML 文档的 <BODY> 文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52426069/