Search code examples
javascriptregexhighlighting

Match suffixes s and ed in regex - javascript


I am using this regex to match words in an html page.

\b(wordtosearch)\b(?!')

The forward negative lookhead is to prevent matching words having ' like don in don't

however i also want to match words that may or may not have s or ed at the end.

like if i have to search for test then it should match tests or tested.

Also please note that wordtosearch can contain many words seperated with | sign i.e car|truck

i realize that it would require a positive lookahead but i dont know how to make all this work.


Solution

  • (wordtosearch)(s|ed)? would do the trick. It will check that the search word is followed by optional s or ed suffix.

    Example:

    /\b(test)(s|ed)?\b(?!')/.test('tested')