JavaScript (jQuery) Regular Expression for Searching an Array
First of all, I don’t know RegEx, but I am trying to piece something together to make this work. Just wanted to be warned.;)
Anyway, I'm trying to create a regex to take a word from an array and see if it matches a word in another array. I want the search to return true if the keyword array string contains the word searchTerm. (i.e. one of them will be false, so it will). Any help POSSIBLY 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) {
var found = false;
$.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) {
found = true;
keywordIndex = i; // grouping keyword is in
return false;
}
}); // end searchTerm loop
if (found) { return false; }
}); // end keyword loop
EDIT: Only keywords [1] will not be executed in this example.
a source to share
This will work:
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].match(rSearchTerm)) {
keywordIndex = i; // grouping keyword is in
alert(keywords[i]); //debug
}
}); // end searchTerm loop
}); // end keyword loop
Two amendments:
- the flag must be a string
"i"
, noti
which is a local int variable. - Backslash require screening because it is part of a string (a literal backslash)
"\\b"
,'\b'
appears as a waste line: ``.
Some notes: I have changes search
before match
(I never used search, so I wanted to make sure it works). Minor optimization. If you change the nesting of loops (for example $.each(searchTerm
), you can create a regex in the outer loop.
a source to share
Another approach, which may also be more efficient, is to concatenate the keywords into a string with unique delimiters, and then execute the regex on that single string. If I'm right at best they have the same efficiency and worst case first solution efficiency is O (n) ^ 2 and that's O (n) (plus O (n) for a single inner loop, do still O (n )).
var searchTerm = ['one','two','three'];
var keywords = ['String which contains one', 'This string is 2', 'Three is here'];
var joinedKey = keywords.join(';');
$.each(searchTerm, function(j) {
var rSearchTerm = new RegExp('\\b' + searchTerm[j] + '\\b','i');
//Find an occurrence of the search term 'j' in the joined string
var index = joinedKey.search(rSearchTerm);
if(index >= 0)
{
//Now if you want to know the item in which the key was found you can calculate length of items.
var len = 0;
for(var pos=0; pos < keywords.length; pos++)
{
len+=keywords[pos];
if(len>index)
{
alert(keywords[pos]);
return true;
}
}
}
}
}); // end searchTerm loop
a source to share
The regex will not search for an array. You need a loop to loop through the elements of the array. Regexp only searches for string values, but can search for numeric values as if they were strings. It looks like you will need to use a regex in your loop to find each index of the array for a specific word.
Well, you only have one global variable to store the indices. It's hard to say what exactly you want, but if it matches the first (i = 0) and it matches the third (i = 2) then "keywordIndex" will just be number 2. Do you want "keywordIndex" to be an array instead ? If yes, then:
var keywordIndex = [];
// ...
if (keywords[i].search(rSearchTerm) > -1) {
keywordIndex.push(i); // grouping keyword is in
}
This will leave you with an array containing the indices for which a match was found.
a source to share