简体   繁体   中英

Match [word] but not [word]apostrophe Regex

my regex is dynamically constructed by using this code in javscript :

var regExp = new RegExp("\\b" + $("#value").val() + "\\b", "ig");

ie the regex would be a simple \\bbrother\\b/ig and the problem is that it is also matching words such as

don in don't
sister in sister's

it should match word only if it is a whole word ie

The don came late   - match
don't do that       - no match
she is my sister    - match
my sister's wedding - no match

EDIT : Thanks for the answers.Please suggest the fastest (or least expensive) method(if this makes an impact) if the regex is very large like \\bbrother|sister|car|truck.......\\b to the tune of 6500 OR words.

There's a few ways you can do it. I would recommend a forward negative lookahead.

\\bbrother\\b(?!')

The construct (?!something) ensures that something does not follow the match.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM