/<[^>]*>/ #matches an HTML tag #So you would want: # s/(<[^>]*>[^<\s]*)\s+/$1\ /g #Should match a tag followed by some non-tag, non whitespace, followed by whitespace. Untested.