Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
59
rated 0 times [  60] [ 1]  / answers: 1 / hits: 18316  / 13 Years ago, sun, january 29, 2012, 12:00:00

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



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


What would be the right regex pattern if I want it to match a sentence that has at least a word starting with key prefix? For example:



If I have a sentence




This is a dog




when key is 'do', then it should match that sentence since prefix 'do' is a substring of 'dog'.



My solution as of now only works for the first word of the sentence. It so far only matches that sentence if I type in 't' or 'th' or 'this'. It wouldnt match that sentence whenever I type in 'i' (prefix for 'is') or 'do' (prefix for 'dog').


More From » regex

 Answers
24

Use the b anchor to match word boundaries:



bdo


finds 'do' in 'nice dog', but doesn't match 'much ado about nothing'.


[#87751] Friday, January 27, 2012, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
miles

Total Points: 256
Total Questions: 111
Total Answers: 104

Location: Benin
Member since Fri, Mar 24, 2023
1 Year ago
;