正则表达式在 JS 文件中查找第一个注释
Regex to find first comment in a JS file
我正在尝试编写一个正则表达式(在JavaScript中),它将匹配JS文件开头的多行注释。
到目前为止,我想出了这个:/^('/'*[^'*'/]*'*'/)/g
它适用于单行注释:https://regex101.com/r/ZS5PVI/1
但我的问题是它不适用于多行评论:https://regex101.com/r/ZS5PVI/2
你有什么想法如何解决吗?
与 HTML 一样,JavaScript 不能通过正则表达式解析。 试图正确地这样做是徒劳的。
相反,您必须使用解析器,该解析器将 JavaScript 源代码正确转换为 AST,您可以通过编程方式检查该 AST 。 幸运的是,有库可以为您解析。
下面是输出此代码的 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 并提取您需要的内容即可。
在这个链接上有一个很好的讨论。这对你有帮助吗?
他的解决方案是:
/'*([^*]|['r'n]|('*+([^*/]|['r'n])))*'*+/
您建议的正则表达式不起作用,因为注释中有*
。此外,它只会查找文件开头的注释。
请尝试改用以下内容:
/'/'*['s'S]*?'*'//
尝试
/'*([^*]|['r'n]|('*+([^*/]|['r'n])))*'*+/
本页详细介绍了如何查找多行注释。
这是一个将匹配任何多行或单行注释:
/('/'*.*?'*'/|'/'/[^'n]+)/
如果你只想要多线比赛,放弃下半场:
/'/'*.*?'*'//
对于这两种情况,请确保设置了s
标志,以便.
与新行匹配。
我不是JavaScript专家,但似乎必须考虑C/C++注释。
正确完成意味着在此过程中必须考虑引号(转义等等)。
以下是两种有效的正则表达式方法。正则表达式 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
- 而循环只设置php中输入字段中的第一个值
- 错误:$injector:modulerr模块错误(我的第一个SPA应用程序)
- IE11中的第二个调用取消了第一个Fetch API调用
- Javascript XMLHttpRequest——只有第一个POST请求有效
- 使用javascript或angularjs特定过滤器搜索字符串中第一个img标记的json值
- 将OnClick函数设置为<ul>,最后一个ul是擦除第一个ul-s
- RxJS油门行为;立即获取第一个值
- Angular UI网格:如何通过第一个UI网格中的按钮使第二个UI网格可见
- 选择多个实例中的第一个
- 如何在调用下一个请求之前完成第一个Ajax Get请求
- Lodash:返回对象的第一个键,该对象的值(即数组)中有一个给定的元素(即字符串)
- 在对象中查找下一个键id
- 看起来第一个console.log是'It’不太对
- 查找上一个<p>在第n个字符之后
- 使用jQuery只查找第一个类
- 在JavaScript中查找数字对列表的最小第一个值
- 仅在第一个li内查找h2
- 如何编写一个选择器来查找第一个“输入”.在类为“x”的元素之后
- 使用jQuery查找任何后代的第一个id
- 使用脚本查找文本字符串,直到第一个html标记