JavaScript 用于在 "commented" 代码中查找模式的正则表达式
JavaScript regular expression to find pattern in "commented" code
我有 JavaScript 个存储在字符串中的片段。我需要查看字符串,例如确定它们是否包含特定代码 "special[i].code"。
我只关心特定代码是否未被注释掉。如果它找到未注释的代码,我需要 return 一个真值,或者只是位置。
存储在字符串中的示例片段。
function someFunction() {
special[i].code
}
RegEx returns: True (Contains uncommitted occurrence)
function someFunction() {
// special[i].code
}
RegEx returns: False (Occurrence commented out)
function someFunction() {
/*
special[i].code
morecode();
*/
}
RegEx returns: False (Occurrence commented out)
function someFunction() {
/* special[i].code */
special[i].code
}
RegEx returns: True (Contains uncommitted occurrence)
我对正则表达式的了解不足以构建正确的表达式。尝试使用编辑器,但因为 // 和 /* 本身是保留字符,即使我转义它们,我也无法正确检测到它。即使当我尝试做类似 //\ 或 //*.
的事情时
如有任何帮助,我们将不胜感激!
你可以用这个。
(注意 - 这是特定于 JavaScript)
匹配注释的困境是你还必须匹配字符串
因为注释部分可以嵌入到字符串中。
鉴于您最终在
内匹配注释、字符串、non-comments、和
non-comments,您需要匹配您的 特殊代码 。
这可能是您想要单匹配所有内容的情况
直到找到您的特殊代码。那么你将有一场比赛
导致 TRUE/FALSE。
你会这么想,并且可以设置一个正则表达式来做到这一点...
但是 如果找不到匹配项,这将导致复杂性问题。
所以,最终发生的是你必须坐在一个循环中,获得所有
部分,直到没有更多的匹配,或,它会找到你的特殊代码
寻找,然后你可以选择突破
不清楚 JS,但如果你不能坐在一个循环中,就一次找到所有,
然后检查捕获缓冲区 3 数组(或者,如果它有一个大
,则每三分之一
数组).
在此正则表达式中,特殊代码(如果找到)将始终在组 3 中。
所以,你只需要检查组 3 是否不为 NULL,设置一个标志,然后跳出循环。
完成后,return 根据是否设置了 标志 做出适当的响应。
这是压缩和分隔的 /../
正则表达式:
/(\/\*[^*]*\*+(?:[^\/*][^*]*\*+)*\/|\/\/[^\n]*(?:\n|$))|("[^"\]*(?:\[\S\s][^"\]*)*"|'[^'\]*(?:\[\S\s][^'\]*)*'|(?!special\[i\]\.code)[\S\s](?:(?!special\[i\]\.code)[^\/"'])*)|(special\[i\]\.code)/
这是 Raw 可读格式的正则表达式(带注释):
( # (1 start), Comments
/\* # Start /* .. */ comment
[^*]* \*+
(?: [^/*] [^*]* \*+ )*
/ # End /* .. */ comment
|
// [^\n]* # Start // comment
(?: \n | $ ) # End // comment
) # (1 end)
| # OR,
( # (2 start), Non - comments
"
[^"\]* # Double quoted text
(?: \ [\S\s] [^"\]* )*
"
|
'
[^'\]* # Single quoted text
(?: \ [\S\s] [^'\]* )*
'
|
(?! special\[i\]\.code ) # This is to blow by any non-comment '/' char or any unbalanced quotes (technically a code error)
[\S\s]
# Any other char, but not these special tokens.
# Chars which doesn't start a comment or string
(?: # But not special tokens
(?! special\[i\]\.code )
[^/"']
)*
) # (2 end)
| # OR,
( # (3 start), Special Tokens
special\[i\]\.code
) # (3 end)
以下是一些具有相应匹配项的示例:
RegEx returns:True(包含未提交的事件)
function someFunction() {
special[i].code
}
** Grp 0 - ( pos 0 , len 31 )
function someFunction() {
** Grp 1 - NULL
** Grp 2 - ( pos 0 , len 31 )
function someFunction() {
** Grp 3 - NULL
-------------------
** Grp 0 - ( pos 31 , len 15 )
special[i].code
** Grp 1 - NULL
** Grp 2 - NULL
** Grp 3 - ( pos 31 , len 15 )
special[i].code
可以在这里中断,因为第 3 组不是 NULL
RegEx returns:True(包含未提交的事件)
function someFunction() {
/* special[i].code */
special[i].code
}
** Grp 0 - ( pos 0 , len 31 )
function someFunction() {
** Grp 1 - NULL
** Grp 2 - ( pos 0 , len 31 )
function someFunction() {
** Grp 3 - NULL
-------------------
** Grp 0 - ( pos 31 , len 21 )
/* special[i].code */
** Grp 1 - ( pos 31 , len 21 )
/* special[i].code */
** Grp 2 - NULL
** Grp 3 - NULL
-------------------
** Grp 0 - ( pos 52 , len 6 )
** Grp 1 - NULL
** Grp 2 - ( pos 52 , len 6 )
** Grp 3 - NULL
-------------------
** Grp 0 - ( pos 58 , len 15 )
special[i].code
** Grp 1 - NULL
** Grp 2 - NULL
** Grp 3 - ( pos 58 , len 15 )
special[i].code
可以在这里中断,因为第 3 组不是 NULL
我有 JavaScript 个存储在字符串中的片段。我需要查看字符串,例如确定它们是否包含特定代码 "special[i].code"。
我只关心特定代码是否未被注释掉。如果它找到未注释的代码,我需要 return 一个真值,或者只是位置。
存储在字符串中的示例片段。
function someFunction() {
special[i].code
}
RegEx returns: True (Contains uncommitted occurrence)
function someFunction() {
// special[i].code
}
RegEx returns: False (Occurrence commented out)
function someFunction() {
/*
special[i].code
morecode();
*/
}
RegEx returns: False (Occurrence commented out)
function someFunction() {
/* special[i].code */
special[i].code
}
RegEx returns: True (Contains uncommitted occurrence)
我对正则表达式的了解不足以构建正确的表达式。尝试使用编辑器,但因为 // 和 /* 本身是保留字符,即使我转义它们,我也无法正确检测到它。即使当我尝试做类似 //\ 或 //*.
的事情时如有任何帮助,我们将不胜感激!
你可以用这个。
(注意 - 这是特定于 JavaScript)
匹配注释的困境是你还必须匹配字符串
因为注释部分可以嵌入到字符串中。
鉴于您最终在
内匹配注释、字符串、non-comments、和
non-comments,您需要匹配您的 特殊代码 。
这可能是您想要单匹配所有内容的情况
直到找到您的特殊代码。那么你将有一场比赛
导致 TRUE/FALSE。
你会这么想,并且可以设置一个正则表达式来做到这一点...
但是 如果找不到匹配项,这将导致复杂性问题。
所以,最终发生的是你必须坐在一个循环中,获得所有
部分,直到没有更多的匹配,或,它会找到你的特殊代码
寻找,然后你可以选择突破
不清楚 JS,但如果你不能坐在一个循环中,就一次找到所有,
然后检查捕获缓冲区 3 数组(或者,如果它有一个大
,则每三分之一
数组).
在此正则表达式中,特殊代码(如果找到)将始终在组 3 中。
所以,你只需要检查组 3 是否不为 NULL,设置一个标志,然后跳出循环。
完成后,return 根据是否设置了 标志 做出适当的响应。
这是压缩和分隔的 /../
正则表达式:
/(\/\*[^*]*\*+(?:[^\/*][^*]*\*+)*\/|\/\/[^\n]*(?:\n|$))|("[^"\]*(?:\[\S\s][^"\]*)*"|'[^'\]*(?:\[\S\s][^'\]*)*'|(?!special\[i\]\.code)[\S\s](?:(?!special\[i\]\.code)[^\/"'])*)|(special\[i\]\.code)/
这是 Raw 可读格式的正则表达式(带注释):
( # (1 start), Comments
/\* # Start /* .. */ comment
[^*]* \*+
(?: [^/*] [^*]* \*+ )*
/ # End /* .. */ comment
|
// [^\n]* # Start // comment
(?: \n | $ ) # End // comment
) # (1 end)
| # OR,
( # (2 start), Non - comments
"
[^"\]* # Double quoted text
(?: \ [\S\s] [^"\]* )*
"
|
'
[^'\]* # Single quoted text
(?: \ [\S\s] [^'\]* )*
'
|
(?! special\[i\]\.code ) # This is to blow by any non-comment '/' char or any unbalanced quotes (technically a code error)
[\S\s]
# Any other char, but not these special tokens.
# Chars which doesn't start a comment or string
(?: # But not special tokens
(?! special\[i\]\.code )
[^/"']
)*
) # (2 end)
| # OR,
( # (3 start), Special Tokens
special\[i\]\.code
) # (3 end)
以下是一些具有相应匹配项的示例:
RegEx returns:True(包含未提交的事件)
function someFunction() {
special[i].code
}
** Grp 0 - ( pos 0 , len 31 )
function someFunction() {
** Grp 1 - NULL
** Grp 2 - ( pos 0 , len 31 )
function someFunction() {
** Grp 3 - NULL
-------------------
** Grp 0 - ( pos 31 , len 15 )
special[i].code
** Grp 1 - NULL
** Grp 2 - NULL
** Grp 3 - ( pos 31 , len 15 )
special[i].code
可以在这里中断,因为第 3 组不是 NULL
RegEx returns:True(包含未提交的事件)
function someFunction() {
/* special[i].code */
special[i].code
}
** Grp 0 - ( pos 0 , len 31 )
function someFunction() {
** Grp 1 - NULL
** Grp 2 - ( pos 0 , len 31 )
function someFunction() {
** Grp 3 - NULL
-------------------
** Grp 0 - ( pos 31 , len 21 )
/* special[i].code */
** Grp 1 - ( pos 31 , len 21 )
/* special[i].code */
** Grp 2 - NULL
** Grp 3 - NULL
-------------------
** Grp 0 - ( pos 52 , len 6 )
** Grp 1 - NULL
** Grp 2 - ( pos 52 , len 6 )
** Grp 3 - NULL
-------------------
** Grp 0 - ( pos 58 , len 15 )
special[i].code
** Grp 1 - NULL
** Grp 2 - NULL
** Grp 3 - ( pos 58 , len 15 )
special[i].code
可以在这里中断,因为第 3 组不是 NULL