0

I want to return a string based on whether or not this plural word exists. If it exists, I want false, but if the singular part of it exists, I want true.

Example:

'cat\fish, cats' returns true because there is 'cat' 'fish, cats' returns false because there is no 'cat' 'cat\fish' returns true because there is no cats.

The statement only returns false if cats is by itself and there is no cat in it.

I tried working with a negative lookahead, /^(?!.*cats).*$/ig but it doesn't correctly recognise the case where there is a cat somewhere in the string.

Thank you!

ionush
  • 323
  • 2
  • 6
  • 12
  • So, you want to match any string that contains a whole word `cat`? `/\bcat\b/i`? Or any word starting with `cat` but not ending with `s`? Try [`/\bcat\B(?!s\b)/i`](https://regex101.com/r/XFRfxp/1) – Wiktor Stribiżew May 29 '18 at 11:19
  • any string that contains the whole word cat. But if cats is there and cat isn't, then don't match. `/\bcat\B(?!s\b)/i.test('zzzzcat')` gives me false. From what I understand, word boundaries limit regex to where there are spaces. This needs to work even if there aren't spaces – ionush May 29 '18 at 11:25
  • Ok, so it should be `/^(?!.*\bcats\b).*\bcat\b/i` - a whole word `cat` but no whole word `cats` is allowed. However, your question sounds rather unclear: I am afraid you misuse the "whole word" concept. – Wiktor Stribiżew May 29 '18 at 11:27
  • Just this one should work: `/cat(?!s)/` – anubhava May 29 '18 at 11:28

1 Answers1

0

You may use this regex using a negative lookahead:

/cat(?!s)/i

RegEx Demo

This regex finds text cat anywhere that must not be followed by a letter s.

Modifier i is used for making it case insensitive.

anubhava
  • 761,203
  • 64
  • 569
  • 643