我需要匹配任意位置不包含关键字的字符串
我需要匹配在任意位置不包含关键字 (beta2
) 的字符串。
考虑一下:
var aStr = [
'/beta1/foo',
'beta1/foo',
'/beta1_xyz/foo',
'blahBlah/beta1/foo',
'beta1',
'/beta2/foo',
'beta2/foo',
'/beta2_xyz/foo',
'blahBlah/beta2/foo',
'beta2',
'/beat2/foo',
'beat2/foo',
'/beat2_xyz/foo',
'blahBlah/beat2/foo',
'beat2'
];
function bTestForMatch (Str)
{
return /.*\b(?!beta2).*/i.test (Str);
}
for (var J=0, iLen=aStr.length; J < iLen; J++)
{
console.log (J, bTestForMatch (aStr[J]), aStr[J]);
}
我们需要一个正则表达式来匹配排除 beta2
的所有字符串。 beta2
将始终从单词边界开始,但不一定以单词边界结束。它可以位于字符串中的多个位置。
期望的结果是:
0 true /beta1/foo
1 true beta1/foo
2 true /beta1_xyz/foo
3 true blahBlah/beta1/foo
4 true beta1
5 false /beta2/foo
6 false beta2/foo
7 false /beta2_xyz/foo
8 false blahBlah/beta2/foo
9 false beta2
10 true /beat2/foo
11 true beat2/foo
12 true /beat2_xyz/foo
13 true blahBlah/beat2/foo
14 true beat2
正则表达式用于第三方分析工具,它采用 JavaScript 正则表达式来过滤子结果。该工具采用单行正则表达式。没有 API,我们也无法访问其源代码。
是否有 JavaScript 正则表达式可以从此分析运行中过滤第二个 beta 结果 (beta2
)?
I need to match strings that don't contain a keyword (beta2
) at an arbitrary position.
Consider:
var aStr = [
'/beta1/foo',
'beta1/foo',
'/beta1_xyz/foo',
'blahBlah/beta1/foo',
'beta1',
'/beta2/foo',
'beta2/foo',
'/beta2_xyz/foo',
'blahBlah/beta2/foo',
'beta2',
'/beat2/foo',
'beat2/foo',
'/beat2_xyz/foo',
'blahBlah/beat2/foo',
'beat2'
];
function bTestForMatch (Str)
{
return /.*\b(?!beta2).*/i.test (Str);
}
for (var J=0, iLen=aStr.length; J < iLen; J++)
{
console.log (J, bTestForMatch (aStr[J]), aStr[J]);
}
We need a regex that matches all strings that exclude beta2
. beta2
will always start at a word boundary, but not necessarily end at one. It can be at a variety of positions in the string.
The desired results would be:
0 true /beta1/foo
1 true beta1/foo
2 true /beta1_xyz/foo
3 true blahBlah/beta1/foo
4 true beta1
5 false /beta2/foo
6 false beta2/foo
7 false /beta2_xyz/foo
8 false blahBlah/beta2/foo
9 false beta2
10 true /beat2/foo
11 true beat2/foo
12 true /beat2_xyz/foo
13 true blahBlah/beat2/foo
14 true beat2
The regex is for a 3rd-party analysis tool that takes JavaScript regular expressions to filter sub-results. The tool takes a single line of regex. There is no API and we don't have access to its source-code.
Is there a JavaScript regex that will filter the second beta results (beta2
) from this analysis run?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试
Try
这会被视为作弊吗?
Would this be considered cheating?