The Pug Automatic

Using gsub with blocks to strip attributes from HTML tags

Written March 3, 2007. Tagged Ruby.

I love Ruby's gsub used with blocks. To strip specified attributes from HTML tags becomes almost too easy:


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

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

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