Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regex to strip all html tags except span tag

Tags:

regex

ruby

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)

like image 813
Madhusudhan Avatar asked Dec 18 '25 05:12

Madhusudhan


1 Answers

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"
like image 162
Chris Heald Avatar answered Dec 20 '25 23:12

Chris Heald