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: