简体   繁体   中英

JavaScript (jQuery) Regular Expression for searching through an array

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. (ie 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.

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.

Another approach that also may be more efficient is to join the keywords to a string with unique separators and then execute the regex on that single string. If I'm correct in best case they have about the same efficiency and in worst case the first solution efficiency is O(n)^2 while this one is O(n) (plus O(n) for the one time inner loop which make it 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 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM