admin管理员组

文章数量:1357568

Say I have an array of words, for example: (hi|ll|this|that|etc) and I want to find it in the following text:

Hi, I'll match this and ll too

I'm using: \\b(hi|ll|this|that|etc)\\b

But I want to only match whole words, excluding words found in contractions. Basically treat apostrophes as another "word seperator". In this case, it shouldn't match the "ll" in "I'll".

Ideas?

Say I have an array of words, for example: (hi|ll|this|that|etc) and I want to find it in the following text:

Hi, I'll match this and ll too

I'm using: \\b(hi|ll|this|that|etc)\\b

But I want to only match whole words, excluding words found in contractions. Basically treat apostrophes as another "word seperator". In this case, it shouldn't match the "ll" in "I'll".

Ideas?

Share Improve this question asked Oct 29, 2015 at 1:53 MaxMax 5194 silver badges7 bronze badges 1
  • Maybe (?:[^']|^)(hi|ll|this|that|etc)\b: regex101./r/gD7aC0/1 – user4227915 Commented Oct 29, 2015 at 2:19
Add a ment  | 

2 Answers 2

Reset to default 4

If you want match just words you can try with:

(?:^|(?=[^']).\b)(hi|ll|th(?:is|at)|etc)\b

DEMO

and get words with group 1. However the \b will still allow to match fragments like: -this or @ll. I don't know is it desired result.

Use the apostrophe in addition to \b to begin and end a match:

(?:\b|')(hi|ll|this|that|etc)(?:\b|')

(?:...) means a non-capturing group. Stub on Regex101

本文标签: javascriptRegular expression match array of words excluding words found in contractionsStack Overflow