从字符串中删除html元素和换行?

时间:2019-04-06 02:45:34

标签: javascript

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

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

但是我很难将regExp删除\ n,这将返回

  

“我的文字\ n”

如何删除文字上的\ n?

1 个答案:

答案 0 :(得分:0)

您只需在正则表达式中使用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, ''))