javascript - How to efficiently check if any substring in an array is contained in another string -
i want have checks if substring in list of strings included in string. have works, i'm hoping there cleaner , more efficient, preferably in 1 line can call if(string.contains(list.any))
or something.
var list = ['aa','bb','cc','dd']; var string = "nygaard"; // true because "aa" in "nygaard". (var = 0; < list.length; i++) { if( string.indexof( list[i] ) > -1 ) { alert("true"); break; } }
why not put loop in own function can call?
function stringcontains(string, list) { (var = 0; < list.length; i++) { if( string.indexof( list[i] ) > -1 ) return true; } return false; }
and call this:
var list = ['aa','bb','cc','dd']; var string = "nygaard"; if(stringcontains(string, list)) alert("true");
if you're looking javascript library function, don't believe there one.
Comments
Post a Comment