Skip to content

Instantly share code, notes, and snippets.

@phudgins
Created January 9, 2012 20:14
Show Gist options
  • Save phudgins/1584704 to your computer and use it in GitHub Desktop.
Save phudgins/1584704 to your computer and use it in GitHub Desktop.
Remove whitespace from HTML tags with only whitespace
str = "<strong> </strong> <p> Hello dere! </p> <ul> <li> </li> <li> I am list </li> </ul>"
# Remove only whitespace
str.gsub(/((<[^>\/]+>)(\s+)(<\/[^>\/]+>))/, '\2\4')
#=> "<strong></strong> <p> Hello dere! </p> <ul> <li></li> <li> I am list </li> </ul>"
# Remove tags that contain only whitespace
str.gsub(/\s*<[^>\/]+>\s+<\/[^>\/]+>\s*/, '')
#=> "<p> Hello dere! </p> <ul><li> I am list </li> </ul>"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment