javascript - Tell If A Multi-Word String Contains A Word -


goal:

(the reason don't think duplicate involves matching start of each word in string, not in string)

i'm using javascript/jquery. have sting, is:

muncie south gateway project 

i'm creating live search box, checks input against string each keystroke. i'd return match if input matches beginning of word, not middle. example:

mu = match muncie = match unc = no match cie = no match gatewa = match atewa = no match 

what have

i using check:

if (new regexp(input)).test(string.tolowercase()) {return '1';} 

however, matches letters including letters in middle of word. it, examples result:

m = match mu = match mun = match muncie = match unc = match // should not match cie = match // should not match gatewa = match atewa = match // should not match 

question:

i know can done breaking string apart separate words , testing each word. i'm not sure how efficient be. there way this?

you can use word boundaries make sure given input matches @ start of word character:

if (new regexp("\\b" + input)).test(string.tolowercase()) {return '1';} 

working demo

edit: per comment below can use:

var re = new regexp("(?:^|\\s)" + input, "i")); if (re.test(string)) {return '1';} 

Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -