I have this regex on mongodb query to match words by prefix:
{sentence: new RegExp(\'^\'+key,\'gi\')}
What would be the right regex pattern
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'
.