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

Bobo
  • 151
  • 1
  • 9

4 Answers4

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]);
  }
}
André Silva
  • 1,149
  • 9
  • 30
  • 1
    your script is good but you forget this: `"\\b" + keywords[i] + "\\b"` to match whole word. – Ghassan Elias Aug 28 '12 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. – André Silva Aug 28 '12 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. – Ghassan Elias Aug 28 '12 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. :) – André Silva Aug 28 '12 at 12:14
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
    }
}
Ghassan Elias
  • 2,213
  • 1
  • 14
  • 17
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

Community
  • 1
  • 1
Srik
  • 7,907
  • 2
  • 20
  • 29
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;
  }
}
Corey G
  • 7,754
  • 1
  • 28
  • 28