Madhusudhan
Madhusudhan

Reputation: 8563

regex to strip all html tags except span tag

I am trying to remove all the html from a string except

<span class="match">...text...</span>

Suppose if a string is "<p>Hello there</p><span class="match">wassup</span>"

I just need "Hello there<span class="match">wassup</span>"

Can anybody help? I am doing it in ruby (rails)

Upvotes: 3

Views: 2040

Answers (1)

Chris Heald
Chris Heald

Reputation: 62648

ActionView::Helpers provides the sanitize method to do this.

http://api.rubyonrails.org/classes/ActionView/Helpers/SanitizeHelper.html#method-i-sanitize

sanitize "some html", :tags => %w"span"

Upvotes: 10

Related Questions