Grails:如何从String中删除任何和所有HTML标记

时间:2022-09-12 18:52:40

What is a simple, fast and reliable way to remove any and all HTML tags from a text string in Grails?

什么是从Grails中的文本字符串中删除任何和所有HTML标记的简单,快速和可靠的方法?

3 个解决方案

#1


This first removes any comments (which might contain tags) and then any tags:

这首先删除任何注释(可能包含标签),然后删除任何标签:

text = text.replaceAll(/<!--.*?-->/, '').replaceAll(/<.*?>/, '')

(via http://grails.1312388.n4.nabble.com/Strip-html-tags-tp1316579p1316580.html)

#2


The best way is to use library Jsoup. Add to dependency:

最好的方法是使用库Jsoup。添加到依赖:

compile 'org.jsoup:jsoup:1.8.1'

And then use it wherever you want. Simply delete all tags, of course, you can configure parsing. The simplest way to delete is:

然后在任何地方使用它。只需删除所有标签,当然,您可以配置解析。最简单的删除方法是:

Jsoup.parse(html).text()

#3


I generally do this by regular expression. If tags are missing below just add them:

我通常通过正则表达来做到这一点。如果下面缺少标签,只需添加它们:

def stripHTMLTags { content ->
  def regex = /<\/?(?i:script|embed|object|frameset|frame|iframe|meta|link|style|a|img|br|p|span|div|hr)(.|\n)*?>/
  content.replaceAll(regex, '')
}

Source: http://grails.1312388.n4.nabble.com/Strip-html-tags-td1316579.html#a1316586

#1


This first removes any comments (which might contain tags) and then any tags:

这首先删除任何注释(可能包含标签),然后删除任何标签:

text = text.replaceAll(/<!--.*?-->/, '').replaceAll(/<.*?>/, '')

(via http://grails.1312388.n4.nabble.com/Strip-html-tags-tp1316579p1316580.html)

#2


The best way is to use library Jsoup. Add to dependency:

最好的方法是使用库Jsoup。添加到依赖:

compile 'org.jsoup:jsoup:1.8.1'

And then use it wherever you want. Simply delete all tags, of course, you can configure parsing. The simplest way to delete is:

然后在任何地方使用它。只需删除所有标签,当然,您可以配置解析。最简单的删除方法是:

Jsoup.parse(html).text()

#3


I generally do this by regular expression. If tags are missing below just add them:

我通常通过正则表达来做到这一点。如果下面缺少标签,只需添加它们:

def stripHTMLTags { content ->
  def regex = /<\/?(?i:script|embed|object|frameset|frame|iframe|meta|link|style|a|img|br|p|span|div|hr)(.|\n)*?>/
  content.replaceAll(regex, '')
}

Source: http://grails.1312388.n4.nabble.com/Strip-html-tags-td1316579.html#a1316586