How to find 2 or more words in a text using jQuery?

2

Hello, I'm doing a search field where it brings the data of an XML.

However, it is only bringing if I type the name in the right order, for example: in xml, the person's name is "Manoel da Silva", but if I only write "Manoel Silva" in the search field, it does not returns the data of the person ... only returns if I put the "da" in the middle of the name ...

Piece of code:

$(xml).find('dado').each(function() {
            var doc      = $(this).find('doc').text(),
                razao    = $(this).find('razao').text(),
                fantasia = $(this).find('fantasia').text(),
                cidade   = $(this).find('cidade').text(),
                uf       = $(this).find('uf').text();

                //var regex = new RegExp(razao, "g");
                //var test = regex.test(nomeFiltro);
                //console.log(test);

            window.indexN = razao.toLowerCase().indexOf(nomeFiltro);

Any ideas?

    
asked by anonymous 10.09.2015 / 20:47

3 answers

2

You can break nomedFiltro into words by making .split(' ') , then for each of these words you create a RegExp with the i (case-insensitive) modifier and check if the text to be tested passes in all validations.

var registro = "Manoel da Silva";
var pesquisa = "mAnOeL SiLvA";

var match = pesquisa.split(" ").every(function (palavra) {
  var regex = new RegExp(palavra, "i");
  return regex.test(registro);
});

alert(match ? "Passou no Teste" : "Não Passou no Teste")
    
10.09.2015 / 21:05
1

do so too:

var string = "manoel da silva";
string.match(/manoel|silva/); // retorna manoel pois existe
string.match(/treco|silva/); // retorna silva pois existe
string.match(/treco|fuleco/); // retorna null pois não existe
    
10.09.2015 / 21:21
1

You could use regular expressions, and some logic to generate the wildcards in your query. Something about this idea (follow the comments to understand the solution):

function prepareQuery(query){
    return query
    	// remover espaços do inicio e fim
        .trim()
    	// troca todos os espaço pelo coringa /.+/
        .replace(/\s/, ".+");
}

function executeQuery(base, query){
    var regex = new RegExp(query, /*i para case-insensitive*/ "i");
    return regex.test(base);
}

// limpa body
document.body.innerHTML = "";
function print(value){
    // para imprimir do DOM
    var element = document.createElement("p");
    element.textContent = value;
    document.body.appendChild(element);
}

function test(){
    var base = "Manoel da Silva";
    var tests = [
        "Manoel Silva",
        "Man Silva",
        "Manoel lva",
        "Manoel a",
        "M Silva",
        "Silva",
        "Manoel",
        "Man va",
        "M a",
        "M",
        "ManoelSilva",
        "Manlva",
        "Ma",
        "Pedro Silva",
    ];
    
    for(var itest in tests){
        var test = tests[itest];
        var query = prepareQuery(test);
        print(test + " : " + executeQuery(base, query));
    }
}
test();
body { white-space: pre; font-family: monospace; }

Example in JSFiddle.

    
10.09.2015 / 22:06