How to invert the results of my RegEx
I have the following RegEx
.*\.(test|spec|es5|es6)\.(js|bundle\.js)
With the following test inputs
bunle.spec.js
my.main.js
test.es6.js
te.es5.js
file.test.js
abc.js
ee.ff.abc.js
eeee.ffff.abc.js
https://regex101.com/r/8BZyA0/2
How to turn my condition a RegEx, so he chooses my.main.js
, abc.js
, ee.ff.abc.js
and eeee.ffff.abc.js
instead?
thank
source to share
You need to convert the first part of the template to negative view and anchor it to the beginning of the line, and - as per the feedback in the comments - you need the line to end with .js
or .bundle.js
- add an anchor $
at the end:
^(?!.*\.(?:test|spec|es[56])\.(?:(?:bundle\.)?js$)).*\.(?:buโโndle\.)?js$
See regex demo .
More details
-
^
- beginning of line -
(?!.*\.(?:test|spec|es[56])\.(?:(?:bundle\.)?js$))
- negative result, which will not match if after 0+ characters other than character interrupt line (due to.*
) the string contains.
, followed bytest
,spec
,es5
ores6
, and then.
, and thenjs
, orbundle.js
at the end of the line ($
) -
.*
- 0+ characters other than line break characters -
\.
- point -
(?:buโโndle\.)?
- an optional non-capturing group that matches 1 or 0 occurrences of the substringbundle.
-
js
- literal substringjs
-
$
- at the end of the line.
source to share