3

I am using this regular expression

/(?!results)/i

As far as I understand, it will match any string that does not contain the word "result". However, when I try

/(?!results)/i.test('basketball results')

it returns true. How do I match strings that do not contain the word results?

Wiktor Stribiżew
  • 561,645
  • 34
  • 376
  • 476
Jürgen Paul
  • 13,381
  • 25
  • 90
  • 131

2 Answers2

4

This regex matches every position that has no results after it. See demo.

To match an expression that does not contain results, you need to use ^(?!.*results.*$).*. See another demo.

Wiktor Stribiżew
  • 561,645
  • 34
  • 376
  • 476
1

You can use a simple indexOf check here. It will return -1 is a substring is not contained in the string and zero or greater otherwise:

"basketball results".indexOf('results') == -1
TimWolla
  • 30,523
  • 8
  • 64
  • 89