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
  • 36,946
  • 82
  • 271
  • 487

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
  • 154,723
  • 16
  • 55
  • 70
1

You can use

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

See the regex demo.

Note:

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563