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

+3


source to share


1 answer


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 by test

    , spec

    , es5

    or es6

    , and then .

    , and then js

    , or bundle.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 substring bundle.

  • js

    - literal substring js

  • $

    - at the end of the line.
+2


source







All Articles