Posted By

nalmeida on 06/04/09


Tagged

replace


Versions (?)

Replace de caracteres especiais por seu equivalente sem acento


 / Published in: JavaScript
 

  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  

You need to login to post a comment.