首页 > 解决方案 > 从字符串中删除 html 元素和新行?

问题描述

我有这段代码可以删除 html 元素:

console.log("<p>Done is beautiful.  <\/p>\n".replace(/<(?:.|\n)*?>/gm, ''))

但我在删除 regExp 时遇到困难\n,这会返回

'我的文字\n'

我如何删除文本上的 \n ?

标签: javascript

解决方案


您可以简单地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, ''))


推荐阅读