regex used in javascript array indexOf
arrays, javascript, regex
Solution
You can also use filter on the array of words,
http://jsfiddle.net/6Nv96/
var str="hello how are you r u fineOr not .Why u r not fine.Please tell wats makes u notfiness";
var splitStr=str.split(" ");
splitStr.filter(function(word,index){
if(word.match(/fine/g)){/*the regex part*/
/*if the regex is dynamic and needs to be set by a string, you may use RegExp and replace the line above with,*/
/*var pattern=new RegExp("fine","g");if(word.match(pattern)){*/
/*you may also choose to store this in a data structure e.g. array*/
console.log(index);
return true;
}else{
return false;
}
});
Problem
I need to find the index of the word in array .But for the following scenario ``` var str="hello how are you r u fineOr not .Why u r not fine.Please tell wats makes u notfiness". var splitStr=str.split(" "); //in splitStr array fineOr is stored at da index of 6. //in splitStr array notfiness is stored at da index of 18. var i=splitStr.indexOf("**fine**"); var k=splitStr.lastindexOf("**fine**"); console.log('value i-- '+i); it should log value 6 console.log('value k-- '+k); it should log value 18 ``` How do I need to pass the regex for searching the string "fine" for the function indexOf of array?