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
  • 607,720
  • 39
  • 448
  • 563
Jürgen Paul
  • 14,299
  • 26
  • 93
  • 133

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
  • 607,720
  • 39
  • 448
  • 563
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
  • 31,849
  • 8
  • 63
  • 96