如何从通过 CKeditor 创建的字符串中完全去除带有换行符的 HTML 标签

How to completely strip HTML tags with newlines after them from string created through CKeditor

这是我的代码:

var htmlString = "<p>ckeditor</p>\n"; 

这是我在 CKeditor 中键入 "ckeditor" 时实际得到的结果。它会自动附加 html 个标签。

我试过了

var string = htmlString.replace(/(<([^>]+)>)/ig,"");

但是,我在 string 中得到的是这样的:

正如你在上面看到的,这里也添加了新行,我只想要没有添加新行的字符串。 请提供解决方案。

要删除编辑器中标记后可能有的任意数量的换行符,您可以在标记模式后添加 \n*

使用

.replace(/<[^>]+>\n*/g, "")

详情:

  • < - 文字 <
  • [^>]+ - > 以外的 1 个或多个字符(因为 [^...] 是一个 否定 字符 class)
  • > - 文字 >
  • \n* - 零个或多个换行符.