Skip to content

Instantly share code, notes, and snippets.

@b1ek
Last active October 13, 2023 14:58
Show Gist options
  • Save b1ek/965cde8637b8ce9fda01ec1de9029e53 to your computer and use it in GitHub Desktop.
Save b1ek/965cde8637b8ce9fda01ec1de9029e53 to your computer and use it in GitHub Desktop.
A simple regex for JS minification

Regex for basic JS minification

This is not exactly a full-featured speedy bundler, but it will do the basic minification.

/((?![\"\'])(\/\/.*|(?<=\n) +|^$|\/\*.*\*\/|(?<=[\{\}\(\)\[\]])[\n ]+|[\n ]+(?=[\{\}\(\)\[\]]))(?![\"\'])|(?<=for) (?=\()|(?<=;)(\n| )| (?=[\=\!\(\)\}\{\"\'\`]))/g
(
    () => {

        // display the js only text
        const text = document.getElementById('drag-n-drop-jsonly');
        text.style.display = 'initial';
// Minified to \/

(
(()=>{
const text= document.getElementById('drag-n-drop-jsonly');text.style.display='initial';

Basically, it just strips unwanted whitespaces and comments ignoring everything that is within " or '.

One knowh caveat is that multiline ` tags will be rendered useless

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