最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Regular expression match array of words excluding words found in contractions - Stack Overflow

programmeradmin0浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论