I'd like to match certain file types (e.g., ".txt") with a non-empty root name that doesn't end in a particular substring (e.g., "-bad"). With negative lookbehind support, the solution is simple:
/.(?<!-bad)\.txt$/
Safari, however, still does not support negative lookbehinds. Ugh. How could I achieve the same result using a negative lookahead? I'd like to do this with a single regular expression. Please do not provide any non-regex or multi-step solutions.
The test code below shows that I'm close, but one test is still failing.
const regex = /.((?!-bad).{4})\.txt$/;
const tests = [
['this-file-bad.txt', false],
['this-file.txt', true],
['.txt', false],
['f.txt', true]
];
const results = tests.map(([input, expected]) => ((regex.test(input) === expected) ? '✅' : '❌') + input);
console.log(results.join('\n'));