RegEx to remove any comments from a JS file

Viewed 84

I try to create a script that will remove any // and /**/ comments from JS files.

The script is currently consists of two regular expressions that are executed one after another. The problem is that it breaks in one specific case and I don't currently know how to fix it.

var input = `
//
// Line comments
//

// aaa

alert("// xxx");

alert(x); // bbb

// ccc

//
// Block comments
//

/*
 * aaa
 * bbb
 */

/* ccc */

/**/ alert(x); /**/

alert("/* xxx */");

alert('/* yyy */');
`;

// removing line comments
input = input.replace(/("[^"]*\/\/.*?")|\/\/(?:.|\r?\n)*?(?:\r?\n|.*)?/g, "$1");

// removing block comments
input = input.replace(/(["'][^"']*\/\*.*?\*\/[^"']*["'])|\/\*(?:.|\r?\n)*?\*\//g, "$1");

console.log(input);

The output from the snippet above is wrong. The line that causes the problem is

alert("// xxx");

The second regex treats the closing quotation mark in that line as the opening one and starts processing from this point. Here is a live demo: https://regexr.com/5n27q

How to fix it?

Credits:

1 Answers

Either:

  1. Don't reinvent the wheel and utilize a package like gulp-strip-comments that already does this.
  2. Or look at the source code of gulp-strip-comments and see how they match every possible form of comment from multiline, to inline, to minified etc. (Source code)

Gulp is your friend if you're trying to "transpile" your JavaScript files. (:

Related