0

When matching "product product name cat1" with this regex I get " product name" with a leading blank, of course I could trim in javascript but is there a way to modify regex to not catch that blank ?

(product)((\s+)(.(?!cat1|cat2))+)
user310291
  • 35,095
  • 76
  • 252
  • 458

2 Answers2

2

Another way to look at it is to use a non greedy quantifier (.*?) in a single capture group, and match either cat1 or cat2 if you are only interested in product name

\bproduct\s+(.*?)\s+cat[12]\b

Regex demo

The fourth bird
  • 127,136
  • 16
  • 45
  • 63
1

You can use

(product)(\s+)((?:(?!cat1|cat2).)+)

See the regex demo.

Note:

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