I have a function that receives values for 2 parameters: string and filter.
I need to check if the string contains the filter value. This is my current code, which works fine:
function matcher(string, filter) {
if(string.search(new RegExp(filter, 'i')) < 0) {
return false;
} else {
return true;
}
}
matcher("french flag", "fren");
But, the matcher function should only return true, if the filter matches the beginning of the string. For example, these cases should be true:
matcher("french flag", "fr");
matcher("french flag", "fre");
matcher("french flag", "frenc");
But these cases should be false (currently the function returns true on these ones):
matcher("french flag", "fla");
matcher("french flag", "ren");
matcher("french flag", "enc");
Thanks for your help.