0
4

3 回答 3

1

你可以打电话给gsub!丢弃所有标签,但只保留 独立的标签,或者不包含在 html 标签中的标签。

result_string.gsub!(/(<\/?[^e][^m]>)|(<<em>\w*<\/em>>)|(<\/<em>\w*<\/em>>)/, '')

会成功的

解释:

# first group (<\/?[^e][^m]>) 
# find all html tags that are not <em> or </em>

# second group (<<em>\w*<\/em>>)
# find all opening tags that have <em> </em> inside of them like:
# <<em>li</em>>   or <<em>ul</em>>

# third group (<\/<em>\w*<\/em>>)
# find all closing tags that have <em> </em> inside of them:
# </<em>li</em>>   or  </<em>ul</em>>

# and gsub replaces all of this with empty string
于 2014-11-25T15:30:12.343 回答
0

我认为您可以使用sinitize

Custom Use (only the mentioned tags and attributes are allowed, nothing else)
<%= sanitize @article.body, tags: %w(table tr td), attributes: %w(id class style) %>

所以,这样的事情应该有效:

sanitize result_string, tags: %w(em)
于 2014-11-25T12:06:37.223 回答
0

使用sanitize的附加参数,您可以指定允许哪些标签。

在您的示例中,请尝试:

ActionController::Base.helpers.sanitize( result_string, tags: %w(em) ) 

它应该可以解决问题

于 2014-11-25T12:06:40.547 回答