Last active
September 3, 2023 22:22
-
-
Save yairEO/9d8e18a330aab275bc86b56075fc234e to your computer and use it in GitHub Desktop.
HTML string minify / compress to single-line
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
/** | |
* @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() | |
: "" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
see: