DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Ruby: Strip Html Tags From A String

07.18.2007
| 23764 views |
  • submit to reddit
        
str = "<html>This and <b>that</b> and <br />and <span class='something'>the other</span>?<html>"
puts str.gsub(/<\/?[^>]*>/, "")

    

Comments

Snippets Manager replied on Sun, 2009/03/01 - 6:48am

Not really. The regex will work as expected with your example since it expects a "greater than" sign at the end (which your example doesn't have). So it actually outputs "x is < than y".

Snippets Manager replied on Sat, 2007/07/21 - 5:27pm

An excellent regex. Best that can be expected without a parser. The one case to be wary of is html that include unescaped less than signs. " x is < than y " => " x is "