admin管理员组

文章数量:1406937

I want regex that will match "lead" or "leads" and will not match when it's part of another word like cheerleaders or leaders. I also don't want whitespaces matched before or after the word.

The closet I got was /(?:^|\W)Lead(?:$|\W){0,5}/g;

But this matches Leaders and whitespaces. This is in javascript if that makes a difference.

I want regex that will match "lead" or "leads" and will not match when it's part of another word like cheerleaders or leaders. I also don't want whitespaces matched before or after the word.

The closet I got was /(?:^|\W)Lead(?:$|\W){0,5}/g;

But this matches Leaders and whitespaces. This is in javascript if that makes a difference.

Share Improve this question asked Aug 13, 2018 at 18:09 VigrantVigrant 1,0182 gold badges18 silver badges29 bronze badges 5
  • is "lead" the only word or do you mean any word? – emsimpson92 Commented Aug 13, 2018 at 18:10
  • lead is the only word. – Vigrant Commented Aug 13, 2018 at 18:11
  • \bleads?\b is what you want then – emsimpson92 Commented Aug 13, 2018 at 18:11
  • Not on my end – emsimpson92 Commented Aug 13, 2018 at 18:12
  • yep that works. go ahead and answer. – Vigrant Commented Aug 13, 2018 at 18:13
Add a ment  | 

1 Answer 1

Reset to default 6

\bleads?\b is all you need. \b is a word boundary, which means the word ends at the boundary.

s? is an optional s

Demo

本文标签: javascriptRegex for a word can be pluralStack Overflow