JavaScript (jQuery) Regular Expression for searching through an array
Posted
by CoryDorning
on Stack Overflow
See other posts from Stack Overflow
or by CoryDorning
Published on 2010-04-23T14:26:05Z
Indexed on
2010/04/23
14:33 UTC
Read the original article
Hit count: 197
JavaScript
|regex
First and foremost, I do not know RegEx but am trying to piece something together to make this work. Just wanted you to be forewarned. ;)
Anyways, I'm trying to create a regular expression to take a word from an array and see if it matches a word in another array. I only want the search to return true if the keyword array string contains the searchTerm word. (i.e. oneone would be false, so would ones). Any help is GREATLY appreciated.
var searchTerm = ['one','two','three'];
var keywords = ['String which contains one', 'This string is 2', 'Three is here'];
var keywordIndex;
// loop through each keyword array
$.each(keywords, function(i) {
$.each(searchTerm, function(j) {
var rSearchTerm = new RegExp('\b' + searchTerm[j] + '\b',i);
// if search term is found, swap accordion div content
if (keywords[i].search(rSearchTerm) > -1) {
keywordIndex = i; // grouping keyword is in
}
}); // end searchTerm loop
}); // end keyword loop
© Stack Overflow or respective owner