1

I need to be able to check for required keyword(s).

Here's what i'm currently using:

    // Check for all the phrases
    for (var i = 0; i < phrases.length; i++) {
      if (value.toLowerCase().indexOf(phrases[i].toLowerCase()) < 0) {
         // Phrase not found
         missingPhrase = phrases[i];
         break;
      }
    }

The problem is that it'll say 'random' is fine if 'randomize' is included.

I found this: how to check if text exists in javascript var (just an example)

But not sure of the best way to incorporate it in jQuery with the possibly to check for multiple keywords.

Community
  • 1
  • 1
Ricky
  • 276
  • 1
  • 7
  • 22
  • Stephen has given you a good idea, but can you give us a sample list of phrases and values, I can give you an example of how to do that. – Adrian Gonzales Feb 20 '11 at 20:48

3 Answers3

1

If you are looking for keywords then why not split() the value on the space character to separate the words into an array. Then loop through the array looking for the keyword.

Taking into account the comments: You should also remove a variety of punctuation characters, maybe replacing them with spaces.

Stephen Perelson
  • 6,613
  • 3
  • 21
  • 15
  • You would also maybe want to eliminate punctuation from the string beforehand so that "random." still matches. – Jacob Feb 20 '11 at 20:50
1

Using the function that you've linked, you can do:

".randomize blah...asd.".containsWord("(hello|blah)")

That's because regexs can match different string as you can see here

In order to make it more readable, you can modify containsWord to accept an array, and escape chars and make the regex based on it.

Good luck!

Community
  • 1
  • 1
Gonzalo Larralde
  • 3,523
  • 25
  • 30
  • Yes but if "hellos" were a substring that pattern would match it, which is not what the asker wants. – Andrew Marshall Feb 20 '11 at 21:02
  • I agree, this way of doing the string match can be a lot neater and is worth pursuing. – Stephen Perelson Feb 20 '11 at 21:03
  • 1
    @Andrew: the regex in the linked example is very precise and won't select anything but a whole word between word boundaries. – Stephen Perelson Feb 20 '11 at 21:05
  • 1
    @Andrew look at the original source of containsWord (linked by @Ricky): var regex = new RegExp( '\\b' + word + '\\b' ); It'll not match. Try it with "random". – Gonzalo Larralde Feb 20 '11 at 21:07
  • It doesn't have to be from that linked example code, I just found it and thought it might help. I'd rather it be based upon the code provided in the question that i'm currently using. Thx for help. – Ricky Feb 20 '11 at 21:14
1
var value_mod = " " + value + " "; // put a fake white space at the beginning and end so we can have a word boundary at the start and end of the line
var regs = [];
var i;
for (i = 0; i < phrases.length; i++) {
    regs.push = new RegExp("\\b" + phrases[i] + "\\b", "i"); /* \b is "word boundary"   "i" for case-insensitive */
}
for (i = 0; i < regs.length; i++) {
     if (regs[i].test(value_mod)) {
        // expression match on whole word.
     } else {
         missingPhrase = phrases[i];
         break;
     }
  }
}
selbie
  • 100,020
  • 15
  • 103
  • 173