¿JavaScript comprueba muchas palabras clave en algún texto?
Frecuentes
Visto 418 veces
1
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
4 Respuestas
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]);
}
}
Respondido 28 ago 12, 13:08
2
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
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
}
}
Respondido 28 ago 12, 12:08
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;
}
}
Respondido 29 ago 12, 16:08
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas javascript google-apps-script or haz tu propia pregunta.
your script is good but you forget this:
"\\b" + keywords[i] + "\\b"
to match whole word. - Ghassan ElíasI didn't forget, I did it on purpose because I thought he was willing to match
abc
inabcde
too. But it is still not wrong, at least can give him a start path. - André Silvai 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