JS ファイルの先頭にある複数行のコメントに一致する正規表現 (JavaScript) を作成しようとしています。
これまでのところ、私はこれを思いつきました:/^(\/\*[^\*\/]*\*\/)/g
単一行のコメントで機能します: http://refiddle.com/24o
しかし、私の問題は、複数行のコメントでは機能しないことです: http://refiddle.com/24m
それを解決する方法はありますか?
JS ファイルの先頭にある複数行のコメントに一致する正規表現 (JavaScript) を作成しようとしています。
これまでのところ、私はこれを思いつきました:/^(\/\*[^\*\/]*\*\/)/g
単一行のコメントで機能します: http://refiddle.com/24o
しかし、私の問題は、複数行のコメントでは機能しないことです: http://refiddle.com/24m
それを解決する方法はありますか?
HTMLと同様に、JavaScriptは正規表現では解析できません。正しく実行しようとしても無駄です。
代わりに、JavaScript ソース コードをプログラムで検査できるASTに正しく変換するパーサーを使用する必要があります。幸いなことに、解析を行うライブラリがあります。
/* this is a
multi-line
comment */
var test = "this is a string, /* and this is not a comment! */";
// ..but this is
それは私たちを得る:
[
"toplevel",
[
[
{
"name": "var",
"start": {
"type": "keyword",
"value": "var",
"line": 5,
"col": 4,
"pos": 57,
"endpos": 60,
"nlb": true,
"comments_before": [
{
"type": "comment2",
"value": " this is a\n multi-line\n comment ",
"line": 1,
"col": 4,
"pos": 5,
"endpos": 47,
"nlb": true
}
]
},
"end": {
"type": "punc",
"value": ";",
"line": 5,
"col": 67,
"pos": 120,
"endpos": 121,
"nlb": false,
"comments_before": []
}
},
[
[
"test",
[
{
"name": "string",
"start": {
"type": "string",
"value": "this is a string, /* and this is not a comment! */",
"line": 5,
"col": 15,
"pos": 68,
"endpos": 120,
"nlb": false,
"comments_before": []
},
"end": {
"type": "string",
"value": "this is a string, /* and this is not a comment! */",
"line": 5,
"col": 15,
"pos": 68,
"endpos": 120,
"nlb": false,
"comments_before": []
}
},
"this is a string, /* and this is not a comment! */"
]
]
]
]
]
]
あとは、AST をループして、必要なものを抽出するだけです。
*
コメントにa があるため、提案された正規表現は機能しません。さらに、ファイルの先頭にあるコメントのみを検索します。
代わりにこれを使用してみてください:
/\/\*[\s\S]*?\*\//
私はJavaScriptの専門家ではありませんが、C /C++のコメントを考慮に入れる必要があるようです。
適切に行われるということは、プロセスで引用符を考慮する必要があることを意味します(エスケープなど)。
以下は、機能する2つの正規表現メソッドです。正規表現1は、最初のCスタイルのコメントを直接検出します。一致するとすぐに検出されます。正規表現2は一般的なケースです。Cスタイル、C ++スタイル、または非コメントのいずれかを検出し、グローバルであり、必要なものを見つけたときに中断できます。
ここでテスト済みhttp://ideone.com/i1UWr
コード
var js = '\
// /* C++ comment */ \\\n\
/* C++ comment (cont) */ \n\
/* t "h /* is" \n\
is first C-style /* \n\
// comment */ \n\
and /*second C-style*/ \n\
then /*last C-style*/ \n\
';
var cmtrx1 = /^(?:\/\/(?:[^\\]|\\\n?)*?\n|(?:"(?:\\[\S\s]|[^"\\])*"|'(?:\\[\S\s]|[^'\\])*'|[^\/"'\\]*))+(\/\*[^*]*\*+(?:[^\/*][^*]*\*+)*\/)/;
var cmtrx2 = /(\/\*[^*]*\*+(?:[^\/*][^*]*\*+)*\/)|(\/\/(?:[^\\]|\\\n?)*?)\n|(?:"(?:\\[\S\s]|[^"\\])*"|'(?:\\[\S\s]|[^'\\])*'|[\S\s][^\/"'\\]*)/g;
//
print ('Script\n===========\n'+js+'\n===========\n\n');
var match;
//
print ("Using Regex 1\n---------------\n");
if ((match=cmtrx1.exec( js )) != null)
print ("Found C style comment:\n'" + match[1] + "'\n\n");
//
print ("Using Regex 2\n---------------\n");
while ((match=cmtrx2.exec( js )) != null)
{
if (match[1] != undefined)
{
print ("- C style :\n'" + match[1] + "'\n");
// break; // uncomment to stop after first c-style match
}
// comment this to not print it
if (match[2] != undefined)
{
print ("- C++ style :\n'" + match[2] + "'\n");
}
}
出力
Script
===========
// /* C++ comment */ \
/* C++ comment (cont) */
/* t "h /* is"
is first C-style /*
// comment */
and /*second C-style*/
then /*last C-style*/
===========
Using Regex 1
---------------
Found C style comment:
'/* t "h /* is"
is first C-style /*
// comment */'
Using Regex 2
---------------
- C++ style :
'// /* C++ comment */ \
/* C++ comment (cont) */ '
- C style :
'/* t "h /* is"
is first C-style /*
// comment */'
- C style :
'/*second C-style*/'
- C style :
'/*last C-style*/'
拡張された正規表現
Regex 1:
/^(?:\/\/(?:[^\\]|\\\n?)*?\n|(?:"(?:\\[\S\s]|[^"\\])*"|'(?:\\[\S\s]|[^'\\])*'|[^\/"'\\]*))+(\/\*[^*]*\*+(?:[^\/*][^*]*\*+)*\/)/
/^
(?:
\/\/
(?: [^\\] | \\\n? )*?
\n
|
(?:
"
(?: \\[\S\s] | [^"\\] )*
"
| '
(?: \\[\S\s] | [^'\\] )*
'
| [^\/"'\\]*
)
)+
1 (
\/\* [^*]* \*+
(?: [^\/*] [^*]* \*+ )*
\/
1 )
/
Regex 2:
/(\/\*[^*]*\*+(?:[^\/*][^*]*\*+)*\/)|(\/\/(?:[^\\]|\\\n?)*?)\n|(?:"(?:\\[\S\s]|[^"\\])*"|'(?:\\[\S\s]|[^'\\])*'|[\S\s][^\/"'\\]*)/g
/
1 (
\/\* [^*]* \*+
(?: [^\/*] [^*]* \*+ )*
\/
1 )
|
2 (
\/\/
(?: [^\\] | \\\n? )*?
2 )
\n
|
(?:
"
(?: \\[\S\s] | [^"\\] )*
"
| '
(?: \\[\S\s] | [^'\\] )*
'
| [\S\s][^\/"'\\]*
)
/g
これは、複数行または単一行のコメントに一致するものです。
/(\/\*.*?\*\/|\/\/[^\n]+)/
複数行の一致だけが必要な場合は、後半を破棄します。
/\/\*.*?\*\//
これらの両方について、新しい行と一致s
するようにフラグが設定されていることを確認してください。.