Regex to match words in a sentence by its prefix

后端 未结 4 1560
礼貌的吻别
礼貌的吻别 2021-01-19 15:25

I have this regex on mongodb query to match words by prefix:

{sentence: new RegExp(\'^\'+key,\'gi\')}

What would be the right regex pattern

4条回答
  •  花落未央
    2021-01-19 16:03

    You can use the expression /\bprefix\w+/. This should match any word starting with "prefix". Here the \b represents a word boundary and \w is any word character.

    If you don't want to get the whole word, you can just do /\bprefix/. If you want to put this in a string, you also have to escape the \: '\\bprefix'.

提交回复
热议问题