1

I have looked at this example and tried using this website, but I am still stuck.

I need to match everything in the node_modules folder except react-native-calendars and it's subdirectories / children.

I think I need to use a Negative Lookahead, here is where I am at:

new RegExp (/node_modules\/(?!.*(react-native-calendars))/)

Any help would be greatly appreciated ! Thank you.

0xPingo
  • 2,047
  • 4
  • 19
  • 43

2 Answers2

1

This works.

Raw ^.*?/node_modules(?:/(?!react-native-calendars(?=/|$))[^/\r\n]*)*$
Regex literal /^.*?\/node_modules(?:\/(?!react-native-calendars(?=\/|$))[^\/\r\n]*)*$/

It matches all items with the /node_modules folder and
sets a validation point at every / to check for the bad folder.

If you are using single item string's, you could remove the (?m).
If you want it to be case insensitive, you could add (?i) if needed.
In JS, you'd add them to the literal syntax /regex/mi

I've already checked this, but if you had some sample input I could link
to an online tester (regex101.com).

Expanded

 ^                             # BOL
 .*?                           # Up until
 /node_modules                 # Required 'node_modules'
 (?:                           # Cluster
      /                             # / folder start
      (?!                           # Assert cannot be the bad folder
           react-native-calendars
           (?= / | $ )                   # Bad folder end
      )
      [^/\r\n]*                     # Ok, get up until start of next folder
 )*                            # Cluster end, do 0 to many times
 $                             # EOL
  • Is this in javacsript? I am getting `Invalid regular expression: /(?m)^.*?\/node_modules(?:\/(?!react-native-calendars(?=\/|$))[^\/\r\n]*)*$/` in my console (same for raw) – 0xPingo Jul 19 '17 at 20:45
  • @pingo - Yeah, just take out the `(?m)` https://regex101.com/r/H2BKQt/2, add it to the delimiter form `//m` if needed. –  Jul 19 '17 at 20:53
0

Assuming that react-native-calendars is a node_modules direct child folder:

/node_modules\/(?!react-native-calendars(?:\/|$)).*/

or

/node_modules\/(?!react-native-calendars(?![^\/])).*/
Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125