views:

149

answers:

3

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) {
  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: In this example, only keywords[1] would fail.

A: 

A regexp will not search an array. You need a loop to cycle through the elements of the array. Regexp only searches string values, but can search number values as though they were strings. It sounds like you will need to use a regexp in your loop to search each array index for a specific word.

i am looping through an array...2 of them in fact.
CoryDorning
A: 

Well you've only got a single global variable to store the indexes. It's hard to tell exactly what it is that you want to happen, but if it matches the first one (i=0) and it matches the third one (i=2), then "keywordIndex" will just be the number 2. Do you want "keywordIndex" to instead be an array? If so, then:

var keywordIndex = [];

// ...

      if (keywords[i].search(rSearchTerm) > -1) {
         keywordIndex.push(i);  // grouping keyword is in
      }

That'll leave you with an array containing the indexes for which a match was found.

Pointy
Sorry...I actually have a return I forgot to include. Updated.
CoryDorning
+2  A: 

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 corrections:

  • the flag should be a string "i", not i, which is a local int variable.
  • Backslash needs escaping, as it is part of a string (literal backslash): "\\b", '\b' comes out as a garbage string: ``.

Some notes: I've changes search to match (I never used search, so I wanted to make sure it works). Minor optimization - If you change the nesting of the loops (eg, $.each(searchTerm first), you can create the regex in the outer loop.

Kobi
Awesome, it works! Thanks. Also, what do you mean for the optimization when you say "you can create the regex in the outer look"?
CoryDorning
@CoryDorning - spelling error. If you move the regex to the outer loop, you create it 3 times instead of 9 times. Again, this is a minor optimization.
Kobi
ah, ok. oversight on my part but you are right. thanks again!
CoryDorning
Just a last note - If you don't need a regex, maybe you shouldn't use one. `'\\b' + str + '\\b'` might fail if the string contains regex characters, for example `four [five] six`. You can use `str1.toLower().search(str2.toLower()) > 1`, or escape all characters in your `searchTerm` array.
Kobi