これをJSに変換する時間がありません。これがPerlサンプルの正規表現ですが、正規表現はJSで動作します。
Cコメント、二重/単一文字列引用符-Jeffrey Friedlによる「ストリップCコメント」から取得され、後でFred Curtisによって変更され、C ++コメントとターゲットセミコロン(私による)を含むように適合されました。
キャプチャグループ1(オプション)。セミコロンまでのすべてが含まれ、グループ2はセミコロンです(ただし、何でもかまいません)。
修飾子は//xsgです。
以下の正規表現は、置換演算子s / pattern / replace / xsgで使用されます(つまり、$ 1 [$ 2]に置き換えます)。
あなたの投稿は、これができるかどうかを調べるためだけのものだと思います。本当に必要な場合は、コメント付きの正規表現を含めることができます。
$str = <<EOS;
testfunc();
testfunc2("test;test");
testfunc3("test';test");
testfunc4('test";test');
//testfunc5();
/* testfunc6(); */
/*
testfunc7();
*/
/*
//testfunc8();
*/
testfunc9("test\"test");
EOS
$str =~ s{
((?:(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/|//(?:[^\\]|\\\n?)*?\n)|(?:"(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|.[^/"'\\;]*))*?)(;)
}
{$1\[$2\]}xsg;
print $str;
出力
testfunc()[;]
testfunc2("test;test")[;]
testfunc3("test';test")[;]
testfunc4('test";test')[;]
//testfunc5();
/* testfunc6(); */
/*
testfunc7();
*/
/*
//testfunc8();
*/
testfunc9("test"test")[;]
コメントで拡大
( ## Optional non-greedy, Capture group 1
(?:
## Comments
(?:
/\* ## Start of /* ... */ comment
[^*]*\*+ ## Non-* followed by 1-or-more *'s
(?:
[^/*][^*]*\*+
)* ## 0-or-more things which don't start with /
## but do end with '*'
/ ## End of /* ... */ comment
|
// ## Start of // ... comment
(?:
[^\\] ## Any Non-Continuation character ^\
| ## OR
\\\n? ## Any Continuation character followed by 0-1 newline \n
)*? ## To be done 0-many times, stopping at the first end of comment
\n ## End of // comment
)
| ## OR, various things which aren't comments, group 2:
(?:
" (?: \\. | [^"\\] )* " ## Double quoted text
|
' (?: \\. | [^'\\] )* ' ## Single quoted text
|
. ## Any other char
[^/"'\\;]* ## Chars which doesn't start a comment, string, escape
) ## or continuation (escape + newline) AND are NOT semi-colon ;
)*?
)
## Capture grou 2, the semi-colon
(;)