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

Asked

Viewed 2,346 times

2

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

However, it is only bringing if I type the name in the right order, for example: in xml, the name of the person is "Manoel da Silva", but if I write only "Manoel Silva" in the search field, it does not return 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 idea?

  • I’m not going to give you the answer, because I’m not sure how to do it, but I think you’re going to have to use something like a query like, follow the documentation http://api.jquery.com/category/selectors/

3 answers

2


you can break the nomedFiltro in words making a .split(' '), then for each of these words you create a RegExp with the modifier i (case-insensitive) and checks whether the text to be tested passes 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")

  • thanks!!! worked :D

  • +1 correct and didactic answer

  • Can you tell me why it returns false if this is so: var record = "MAICON KNABACH FURTADO numerocpf"; var search = "Maicon Furtado"; ?? numerocpf = my own Cpf... put this so as not to expose my data heheh

  • Could you update the question with this excerpt of codico? if you prefer you can even bang with false information. is bad to see code in a comment.

  • I think the problem is in the same backend.. even heat.. Thanks for the help!

  • The Every function of the array would be the same thing as a foreach?

  • no, when using the forEach, you will always go through all the elements besides being able to modify them, Every will perform a check for each element of the array, so the function it receives returns a boolean, in case some element does not pass the test (Return false) it will not test the next elements and return false, if all elements pass the test it returns true.

Show 2 more comments

2

You could use regular expressions, and some logic to generate wildcards in your query. Something in 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.

1

p/ 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.