我需要匹配任意位置不包含关键字的字符串

发布于 2024-09-10 18:02:54 字数 1511 浏览 4 评论 0原文

我需要匹配在任意位置包含关键字 (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

美胚控场 2024-09-17 18:02:54

尝试

/^(?!.*beta2).*$/

Try

/^(?!.*beta2).*$/
素罗衫 2024-09-17 18:02:54

这会被视为作弊吗?

return !/beta2/i.test (Str);

Would this be considered cheating?

return !/beta2/i.test (Str);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文