-1

I want to exclude file paths that have capsize anywhere in the path in node or javascript but I am failing to get the syntax right.

const re = /.*((?!capsize).*)\.css\.ts/;

console.log(re.test('src/packages/graph.css.ts')) // should be true

console.log(re.test('packages/vanilla-extract/capsize.css.ts')) // should be false
Barmar
  • 741,623
  • 53
  • 500
  • 612
dagda1
  • 26,856
  • 59
  • 237
  • 450

2 Answers2

2

First you need to specify, that you want to match whole string, ie. add start and end mark. (Otherwise it will skip .* as no symbol were matched, which is ok from regex point of view)

Second you need to put not follow by right after any symbol (ie. saying, there can be 0 to infinity symbols not followed by capsize).

const re = /^(.(?!capsize))*\.css\.ts$/;

console.log(re.test('src/packages/graph.css.ts')) // should be true

console.log(re.test('packages/vanilla-extract/capsize.css.ts')) // should be false
SergeS
  • 11,533
  • 3
  • 29
  • 35
1

One possible way:

const re = /^(?!.*?capsize).*\.css\.ts$/;

console.log(re.test('src/packages/graph.css.ts')) // should be true

console.log(re.test('packages/vanilla-extract/capsize.css.ts'))
raina77ow
  • 103,633
  • 15
  • 192
  • 229