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
2 Answers
Reset to default 4If 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
版权声明:本文标题:javascript - Regular expression match array of words excluding words found in contractions - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744015888a2576320.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论