Skip to content

Instantly share code, notes, and snippets.

@xulapp
Created March 28, 2010 04:50
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xulapp/346584 to your computer and use it in GitHub Desktop.
Save xulapp/346584 to your computer and use it in GitHub Desktop.
JS のコメントを除去
// できないこと
// ・演算子(/, /=) の後ろにあるコメントを除去できない
// ・演算子(/, /=) の後ろにある正規表現リテラルを無視できない
// ・<xml>{0/*消えて*/}/*消えないで*/</xml>
function trimComment(str) {
// コメント___________________ 文字列リテラル(")_______ 文字列リテラル(')_______ CDATA セクション________ 正規表現リテラル___________________________
return str.replace(/(\/)(?:\*[\s\S]*?\*\/|\/.*)|"(?:\\[\s\S]|[^\\\n"])*"|'(?:\\[\s\S]|[^\\\n'])*'|<!\[CDATA\[[\s\S]*?\]\]>|\/(?:\\.|\[(?:\\.|[^\n\]])*\]|[^\n/])+\/\w*/g, function($0, $1) {
return $1 ? '' : $0;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment