This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the regex category.
Last Updated: 2024-11-23
I wanted to match files ending in .js
but not ones ending in .spec.js
e.g to match users.js
but not users.spec.js
My first draft using a negative lookahead revealed a misunderstanding about lookaheads:
/\w+(?!spec)\.js/
When called on users.spec.js
it returned spec.js
- i.e. the \w+
bit
captured spec
despite the negative lookahead asking not to include that bit.
Essentially the issue was that the negative lookahead applied only after the
\w+
To understand more:
"users.spec.js".match(/(?!spec)\.js/)
returns .js
"users.spec.js".match(/.+(?<!spec)\.js/)
returns null
(i.e this works for
all our purposes )Another, probably easier way to get it to work, is to rewrite with a negative lookbehind:
/\w+(?<!spec)\.js$/
Watch out for prior greediness crowding out negative lookahead. Consider replacing problematic parts with negative look-behinds.