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

Using Gsub With Blocks To Strip Attributes From HTML Tags

03.18.2007
| 4097 views |
  • submit to reddit
        originally post here:
http://henrik.nyh.se/2007/03/03/using-gsub-with-blocks-to-strip-attributes-from-html-tags/
html = 'Getting <a href="#" id="foo">rid</a> of <code id="bar">id
 attributes, but not in text: id="not this".' 

html.gsub(/<(.*?)>/) {|innards| innards.gsub(/ id=("|').*?\1/, '') }

# => Getting <a href="#">rid</a> of 
id
 attributes, but not in text: id="not this".