Created
January 9, 2012 20:14
-
-
Save phudgins/1584704 to your computer and use it in GitHub Desktop.
Remove whitespace from HTML tags with only whitespace
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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