-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
  • 669,327
  • 51
  • 454
  • 560
dagda1
  • 23,659
  • 54
  • 216
  • 399

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
  • 10,948
  • 2
  • 28
  • 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
  • 99,006
  • 14
  • 190
  • 222