Skip to content

Instantly share code, notes, and snippets.

@davidhq
Last active December 26, 2019 07:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidhq/1ca7112f589fb6791a317cd40310103e to your computer and use it in GitHub Desktop.
Save davidhq/1ca7112f589fb6791a317cd40310103e to your computer and use it in GitHub Desktop.
index.js
if ('a'.replace(/\//g, '')) {
// something
}
const fs = require('fs');
const re1 = /((?:(?:^\h*)?(?:\/\*[^*]*\*+(?:[^\/*][^*]*\*+)*\/(?:\h*\n(?=\h*(?:\n|\/\*|\/\/)))?|\/\/(?:[^\\]|\\\n?)*?(?:\n(?=\h*(?:\n|\/\*|\/\/))|(?=\n))))+)|("(?:\\[\S\s]|[^"\\])*"|'(?:\\[\S\s]|[^'\\])*'|[\S\s][^\/"'\\\s]*)/gm;
const re2 = /((?:(?:^[ \t]*)?(?:\/\*[^*]*\*+(?:[^\/*][^*]*\*+)*\/(?:[ \t]*\r?\n(?=[ \t]*(?:\r?\n|\/\*|\/\/)))?|\/\/(?:[^\\]|\\(?:\r?\n)?)*?(?:\r?\n(?=[ \t]*(?:\r?\n|\/\*|\/\/))|(?=\r?\n))))+)|("(?:\\[\S\s]|[^"\\])*"|'(?:\\[\S\s]|[^'\\])*'|(?:\r?\n|[\S\s])[^\/"'\\\s]*)/gm;
const code = fs.readFileSync('./code.js').toString();
console.log(code.replace(re1, '$2'));
console.log(code.replace(re2, '$2'));
@davidhq
Copy link
Author

davidhq commented Dec 18, 2019

$ node index.js

if ('a'.replace(/\

}

if ('a'.replace(/\
}

This is wrong. Re/ https://stackoverflow.com/a/59147511/458177

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