Ignore accent when using search menu (input)

Asked

Viewed 325 times

-1

I created a input search and a list (side menu) below, when I do the search is ok, but I’m in trouble when any <li> has voice with accentuation.

How could I implement a regular expression to help me?

$(function() {
    $("#buscar-menu").keyup(
        function() {
            var menuBuscado = $(this).val().toUpperCase();
                $(".menu-busca li").css("display", "block");
                $(".menu-busca li").each(function() {
                            if ($(this).text().toUpperCase().indexOf(
                                    menuBuscado.toUpperCase()) < 0) {
                                $(this).css("display", "none");
                            }
                });
        });
});

1 answer

1


It is possible to create a function for this particular case, which would remove the accents only in the query, in the example below I entered a function called removeAcento that will receive a parameter(string) and return the same string without the accents, just do this in the input typed and the text recovered within the function each jquery.

Making it look like this:

function removeAcento(text){       
    text = text.toLowerCase();                                                         
    text = text.replace(new RegExp('[ÁÀÂÃ]','gi'), 'a');
    text = text.replace(new RegExp('[ÉÈÊ]','gi'), 'e');
    text = text.replace(new RegExp('[ÍÌÎ]','gi'), 'i');
    text = text.replace(new RegExp('[ÓÒÔÕ]','gi'), 'o');
    text = text.replace(new RegExp('[ÚÙÛ]','gi'), 'u');
    return text;                 
}

$(function() {
  $("#buscar-menu").keyup(
    function() {
      var menuBuscado = removeAcento($(this).val().toUpperCase());
      $(".menu-busca li").css("display", "block");
      $(".menu-busca li").each(function() {
      if (removeAcento($(this).text()).toUpperCase().indexOf(
        menuBuscado.toUpperCase()) < 0) {
        $(this).css("display", "none");
      }
    });
  });
});

Reference:

Remove Accents/diacritics in a string in Javascript

glmxndr

  • I wasn’t going to deny it, but forgive me, I had, because I find it very incorrect the attitude of some users to take something ready and paste in the reply and not at least credit the authors or the original post that helped you in part of the code. I ask you to always do this, because people deserve some recognition, as if you formulate a code and someone else should also use it, it is not a rule, but it is a matter of cordiality. I promise to even give you credit if you do and please don’t take it personally, it’s just constructive criticism.

  • Another suggestion, this code is not good, I recommend trying others more complete, which include ç or by using NFD (see about NFD and others on https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/normalize)

  • @Guilhermenascimento may have been careless and ignorant of the "creator" of this function on my part, but I have no idea who the author is, I’ve been using this function in my code for years to correct this same problem from the question above, for the next questions before using a function I did not create I will search if I find the author, often it may be even difficult by the amount of duplicated function that should exist, but thanks for the advice of quoting the author, I will pay more attention to that, constructive criticism is always welcome!

  • In codecs nothing is perfect to control, unless everything is very well done on the part of the author of the question, of course, but anyway a suggestion that can solve better, higher, minusculos, utf vs iso-8859-1/latin and others would be the use of this https://stackoverflow.com/a/37511463/1518921, if you edit the answer I can remove the downvote, because after 40 min it is no longer possible to remove unless q vc edit, it is a system lock. I stand by

  • 1

    Done, added as reference to code

Browser other questions tagged

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