I need to match strings that don't contain a keyword at an arbitrary position

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?

Answers:

Answer

Try

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

Would this be considered cheating?

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

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.