I will need your help to extract string pattern inside the JavaScript variable from the array.
For example, lets say:
var type = ["men","women","boys", "girls"]
var product = "women shoes"
I would like to write the function that search the variable product from my custom define variable type.
So the function can return the string "women".
Many thanks!!!
Allen
I'd go with:
var type = ["men", "women", "boys", "girls"],
product = "women shoes",
result;
function filterData(item, index) {
if (new RegExp( '\\b' + item + '\\b', 'i').test(product)) {
return true;
}
return false;
}
result = type.filter(filterData);
console.log(result);
Working example: https://jsfiddle.net/nc1qodk5/
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments