1

Please, whats the best way for checking if some text sentence have some of the keywords provided, and there could be 100 keywords.

For example:

var text = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";
var keywords = ["lorem", "sit", "elit", "sed do"];

Now whats the best and fastest way to check if any of keywords is in text, but like whole word not part of word, and its case-insensitive?

BTW Its Google Script, maybe there is some function which I didnt see :\

Thanks in advance

4 Answers 4

3
for(var i = 0; i <= keywords. length - 1; i++) {
  var a = new RegExp('\\b' + keywords[i] + '\\b', 'gi');
  if(a.test(text)) {
    alert('Match ! - ' + keywords[i]);
  }
}
4
  • 1
    your script is good but you forget this: "\\b" + keywords[i] + "\\b" to match whole word. Aug 28, 2012 at 11:42
  • I didn't forget, I did it on purpose because I thought he was willing to match abc in abcde too. But it is still not wrong, at least can give him a start path. Aug 28, 2012 at 12:00
  • 1
    i know it's not wrong :) but he wanted to search and match the whole word as he mentioned in his question. Aug 28, 2012 at 12:08
  • Going to edit. I'm sorry about my lack of attention, so hard to answer stuff with boss walking around. Thanks for the heads up. :) Aug 28, 2012 at 12:14
2

Try something on these lines.

var text = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";
var keywords = ["lorem", "sit", "elit", "sed do"];
var words = text.split(/[\s,]+/); //Add all other delimiters you want to include. 
for(var i in words){ 
   words[i] = words[i].toLowerCase(); 
}
for(var i in keywords){
  if(words.indexOf(keywords[i].toLowerCase()) != -1){
    Logger.log('Match found for ' + keywords[i]);
    break;
  }
}

Thanks to this post for splitting on multiple delimiters

1
  • 1
    fix the syntax errors. var keywords = ["lorem","etc.."] and if(words.indexOf(keywords[i].toLowerCase()) != -1){ Aug 28, 2012 at 11:56
2
var text = "Lorem Ipsumdolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";
var keywords = ["Lorem", "ipsum", "not there", "Sed do"];

for (var i in keywords) {
    var index = new RegExp("\\b" + keywords[i] + "\\b", "i");
    if (text.match(index)) {
        alert(keywords[i]);
        // do something
    }
}
0

This code will be vastly more efficient if you use an object to store the keywords rather than an array.

var text = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";
var keywords = {"lorem" : 1, "sit": 1, "elit" : 1, "sed do" : 1};
var words = text.split(/[\s,]+/); //Add all other delimiters you want to include. 
for(var i in words) { 
   if (keywords[words[i].toLowerCase()]) { 
     Logger.log('Match found for ' + keywords[i]);
     break;
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.