Replace de caracteres especiais por seu equivalente sem acento


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. /**
  2.  * Array de objectos de qual caracter deve substituir seu par com acentos
  3.  */
  4. var specialChars = [
  5. {val:"a",let:"áàãâä"},
  6. {val:"e",let:"éèêë"},
  7. {val:"i",let:"íìîï"},
  8. {val:"o",let:"óòõôö"},
  9. {val:"u",let:"úùûü"},
  10. {val:"c",let:"ç"},
  11. {val:"A",let:"ÁÀÃÂÄ"},
  12. {val:"E",let:"ÉÈÊË"},
  13. {val:"I",let:"ÍÌÎÏ"},
  14. {val:"O",let:"ÓÒÕÔÖ"},
  15. {val:"U",let:"ÚÙÛÜ"},
  16. {val:"C",let:"Ç"},
  17. {val:"",let:"?!()"}
  18. ];
  19.  
  20. /**
  21.  * Função para substituir caractesres especiais.
  22.  * @param {str} string
  23.  * @return String
  24.  */
  25. function replaceSpecialChars(str) {
  26. var $spaceSymbol = '-';
  27. var regex;
  28. var returnString = str;
  29. for (var i = 0; i < specialChars.length; i++) {
  30. regex = new RegExp("["+specialChars[i].let+"]", "g");
  31. returnString = returnString.replace(regex, specialChars[i].val);
  32. regex = null;
  33. }
  34. return returnString.replace(/\s/g,$spaceSymbol);
  35. };

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.