¿JavaScript comprueba muchas palabras clave en algún texto?

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

Por ejemplo:

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 no distingue entre mayúsculas y minúsculas?

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

Gracias por adelantado

preguntado el 28 de agosto de 12 a las 12:08

4 Respuestas

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]);
  }
}

Respondido 28 ago 12, 13:08

your script is good but you forget this: "\\b" + keywords[i] + "\\b" to match whole word. - Ghassan Elías

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

i know it's not wrong :) but he wanted to search and match the whole word as he mentioned in his question. - Ghassan Elías

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

Prueba algo en estas líneas.

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;
  }
}

Gracias a esta publicación for splitting on multiple delimiters

contestado el 23 de mayo de 17 a las 13:05

fix the syntax errors. var keywords = ["lorem","etc.."] y if(words.indexOf(keywords[i].toLowerCase()) != -1){ - Ghassan Elías

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
    }
}

Respondido 28 ago 12, 12:08

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;
  }
}

Respondido 29 ago 12, 16:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.