javascript - 使用 REGEX 在节点 js 中提取 HTML 文档的文本
问题描述
我正在编写一个代码来从 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
}
解决方案
推荐阅读
- python - 使用 tensorflow 将 protobuf 升级为警告
- python - 为什么我解码的 Windows-1252 字符串在字典中显示为 unicode 值而不是该值,尽管我尝试将其解码为 UTF-8?
- mysql - LINQ 查询语法操作数应包含 1 列
- javascript - 在 vue.js 中执行 axios.get 时捕获特定元素
- c++ - 将 QTreeView 填充为数据库中的父节点和子节点
- mysql - Wordpress 容器拒绝与 mysql 连接
- r - R:根据不同的值生成表
- c# - 在位图 500x500 上按角度绘制线条
- flutter - -bash:颤振:找不到命令
- xamarin.forms - 可绑定的 ObservableCollection 不更新日历 UI