Skip to content

Instantly share code, notes, and snippets.

@yairEO
Last active September 3, 2023 22:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yairEO/9d8e18a330aab275bc86b56075fc234e to your computer and use it in GitHub Desktop.
Save yairEO/9d8e18a330aab275bc86b56075fc234e to your computer and use it in GitHub Desktop.
HTML string minify / compress to single-line
/**
* @param {string} s [HTML string]
*/
function minify( s ){
return s ? s
.replace(/\>[\r\n ]+\</g, "><") // Removes new lines and irrelevant spaces which might affect layout, and are better gone
.replace(/(<.*?>)|\s+/g, (m, $1) => $1 ? $1 : ' ')
.trim()
: ""
}
@yairEO
Copy link
Author

yairEO commented Dec 16, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment