Skip to content

Instantly share code, notes, and snippets.

@rafbm
Created August 27, 2011 19:26
Show Gist options
  • Save rafbm/1175769 to your computer and use it in GitHub Desktop.
Save rafbm/1175769 to your computer and use it in GitHub Desktop.
CSS compressor
var compressCSS = function(css) {
return css.trim()
.replace(/\s*([{}:;,>+~])\s*/g, '$1') // whitespace
.replace(/;}/g, '}') // trailing semicolons
.replace(/#(\w)\1(\w)\2(\w)\3\b/g, '#$1$2$3') // hex colors
.replace(/(\[[^=]+=)("|')([^"'\s]+)(\2)(\])/g, '$1$3$5') // useless attribute selector quotes
.replace(/\(('|")([^"'\s]+)(\1)\)/g, '($2)') // useless function quotes
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment