javascript - 从字符串中删除 html 元素和新行?
问题描述
我有这段代码可以删除 html 元素:
console.log("<p>Done is beautiful. <\/p>\n".replace(/<(?:.|\n)*?>/gm, ''))
但我在删除 regExp 时遇到困难\n,这会返回
'我的文字\n'
我如何删除文本上的 \n ?
解决方案
您可以简单地alternation ( | )
在您的正则表达式中使用
/<(?:.|\n)*?>|\n/
| |________ Matches new line
|__________________ Matches `<` followed by any character followed by `>`
console.log('<p>my text</p>\n some value'.replace(/<(?:.|\n)*?>|\n/gm, ''))
推荐阅读
- nhibernate - NHibernate show_sql 不显示所有插入
- javascript - 仅通过特定键、值对的错误值过滤数组
- python - ValueError: Found array with dim 3. Estimator expected <= 2. 如何在随机森林回归器上使用它?
- flutter - Flutter 中的自定义 SliverAppBar
- javascript - Protractor 中的 Promise 是如何解决的?
- python - 子类 __init__ 中的 NameError:未定义“自我”
- html - 响应式 iframe 仍然有滚动条并且没有缩放
- python - Selenium:“WebDriver”对象没有属性“Chrome”(将桌面更改为移动设备)
- asp.net-core - signalR - 向特定用户发送消息
- node.js - 如何在 PouchDB 中使用 medeadown 作为 leveldown 的替代方法