Handle accent with Jquery

Asked

Viewed 1,413 times

1

I’m having trouble treating stress in jQuery.

I would like to remove word accents and replace spaces with "%20". I tried to do it the way down, but it didn’t work.

cidade_sem_tratar = $('#estados').val();
console.log(cidade_sem_tratar);

cidade_tratada = cidade_sem_tratar
.replace(/[áàâã]/,'a')
.replace(/[éèê]/,'e')
.replace(/[óòôõ]/,'o')
.replace(/[úùû]/,'u')
.replace(' ','%20');
console.log(cidade_tratada);

The result is as follows:

problema de acentuação com jQuery

Please, could you help me?

Hug and Thanks!

3 answers

1

Your code is correct in replacing accents, but to replace ALL spaces, change the last one replace for:

.replace(/\s/g,'%20')

As it stands, it will replace only the first space found.

See working:

cidade_sem_tratar = $('#estados').val();
console.log(cidade_sem_tratar);

cidade_tratada = cidade_sem_tratar
.replace(/[áàâã]/,'a')
.replace(/[éèê]/,'e')
.replace(/[óòôõ]/,'o')
.replace(/[úùû]/,'u')
.replace(/\s/g,'%20');
console.log(cidade_tratada);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="estados" value="Afonso Cláudio" />

Using Function

function remAcentos(v){
   var c='áàãâäéèêëíìîïóòõôöúùûüçÁÀÃÂÄÉÈÊËÍÌÎÏÓÒÕÖÔÚÙÛÜÇ';
   var s='aaaaaeeeeiiiiooooouuuucAAAAAEEEEIIIIOOOOOUUUUC';
   var n = '';
   for(var x=0;x<v.length;x++){
      c.search(v.substr(x,1))>=0 ?
      n+=s.substr(c.search(v.substr(x,1)),1) :
      n+=v.substr(x,1);
   }
   return n;
}

cidade_sem_tratar = $('#estados').val();
console.log(cidade_sem_tratar);
cidade_tratada = remAcentos(cidade_sem_tratar).replace(/\s/g,'%20');
console.log(cidade_tratada);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="estados" value="Afonso Cláudio" />

  • To treat the words with accentuation is not working.. but in relation to the spacing is show! : ) Mto thanks

  • Yeah. I’m finding this very strange mistake. I saw that it worked in your simulation.. but for me it appears as "Afonso%20Cláudio". Acute accent in "á".

  • I don’t know if this information is important, but, the data I’m getting from a JSON file.

  • On inspection appears without accent.. <a tabindex="0" class="" data-normalized-text="<span class=&quot;text&quot;>Afonso Claudio</span>"><span class="text">Afonso Cláudio</span><span class="glyphicon glyphicon-ok check-mark"></span></a>

  • I wonder what might be going on?

  • It is a city selector. It is being built through a JSON file.

  • @Diegoalbuquerque I think it won’t work no. I had to see the code of how you tah taking the xml data. That’s the secret.

  • That part helps? $. each(json.states[_idState]. cities, Function(Indice)ː $('#states'). append( '<option value="' + json.states[_idState]. cities[Indice] + '" ' + (json.states[_idState]. cities[Indice] == _city ? 'Selected="true"' : """) + 'data-Uf="' + json.states[_idState]. acronym + '"' + '>' + json.states[_idState]. cities[Indice] + '</option>'); });

  • The case was solved!! Thank you so much for the support and attention!! : ) Rui Ferreira gave a cool tip! A hug

  • @Diegoalbuquerque If you found the answer useful, be sure to mark ✔

Show 5 more comments

1

Check that the encodeURI function does what it wants, for example:

var myStr = "Afonso Cláudio";
var res = encodeURI(myStr);

function to remove accents:

function removerAcentos( newStringComAcento ) {
  var string = newStringComAcento;
  var mapaAcentosHex    = {
    a : /[\xE0-\xE6]/g,
    A : /[\xC0-\xC6]/g,
    e : /[\xE8-\xEB]/g,
    E : /[\xC8-\xCB]/g,
    i : /[\xEC-\xEF]/g,
    I : /[\xCC-\xCF]/g,
    o : /[\xF2-\xF6]/g,
    O : /[\xD2-\xD6]/g,
    u : /[\xF9-\xFC]/g,
    U : /[\xD9-\xDC]/g,
    c : /\xE7/g,
    C : /\xC7/g,
    n : /\xF1/g,
    N : /\xD1/g
};

for ( var letra in mapaAcentosHex ) {
    var expressaoRegular = mapaAcentosHex[letra];
    string = string.replace( expressaoRegular, letra );
}

return string;
}

You can use the remove function and then encodeURI to put %20 in the spaces

  • I tried it but it didn’t work.. when it will try to use the city name after going through the Ncode it appears with accent in the URL. I’m trying to find a pattern for the site: http://servicos.cptec.inpe.br/XML/listaCidades?city=Afonso%20claudio .... I get the name of the cities with accents, then I treat and then search. The problem is happening to treat city names in the way that the site accepts (without accentuation). Thanks for the help

  • How the post is doing?

  • 1

    It worked!!!!!!!!! Thank you very much!!!

  • I’m glad it worked ;)

0

replace in javascript works with regex, and vc n is passing a regex to swap the spaces. tries to put like this:

string.replace(/ /g, '%20');

You can test regex on websites like this, for example: https://regex101.com/

Browser other questions tagged

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