0

I was digging around in the example projects in the Next.js project and in the Jest configuration for the Typescript project I found this pattern in the testPathIgnorePatterns in the Jest configuration:

<rootDir>[/\\\\](node_modules|.next)[/\\\\]

What exactly is this? Looks somewhat like a regular expression, but I don't understand the [/\\\\] part?

I would like to extend the expression above with a positive lookahead in order to exclude certain node modules from being ignored, but I'm not quite sure how to achieve this?

skyboyer
  • 22,209
  • 7
  • 57
  • 64
zkwsk
  • 1,960
  • 4
  • 26
  • 34
  • 1
    It matches a slash or a backslash, i.e. path separators on Windows or not; the backlashes have to be escaped twice for JSON string and regex syntax. – jonrsharpe Nov 25 '20 at 15:27
  • https://stackoverflow.com/questions/10769964/backslashes-regular-expression-javascript is related question but not a dupe. – Estus Flask Nov 25 '20 at 22:29

1 Answers1

0

This is regular expression, where \\\\ means a single \, so [/\\\\] character class means either / slash or \ backslash character.

The context for it is that it's provided as a string in Jest JSON configuration:

"testPathIgnorePatterns": "<rootDir>[/\\\\](node_modules|.next)[/\\\\]"

The string is supposed to be passed to new RegExp constructor then, with <rootDir> placeholder being replaced by actual path. By being used this way, \ character needs to be escaped twice - once as JavaScript string:

"\\"; // a backslash
"\"; // syntax error

And another time as regular expression:

/^[\\]$/.test("\\") // true
/^[\]$/.test("\\") // syntax error
Estus Flask
  • 206,104
  • 70
  • 425
  • 565