Last active
December 26, 2015 22:09
-
-
Save felquis/7220721 to your computer and use it in GitHub Desktop.
Retira caracteres especiais para serem usados como URLs, ou para qualquer outra como que não seja aceito caracteres especiais como letras acentuadas ou interrogações entre outros.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var url = toUrl('Você mora em São Paulo?'); | |
console.log(url); // voce-mora-em-sao-paulo |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function toUrl(strToReplace) { | |
str_acento = "áàãâäéèêëíìîïóòõôöúùûüçÁÀÃÂÄÉÈÊËÍÌÎÏÓÒÕÖÔÚÙÛÜÇ"; | |
str_sem_acento = "aaaaaeeeeiiiiooooouuuucAAAAAEEEEIIIIOOOOOUUUUC"; | |
var nova = ''; | |
for (var i = 0; i < strToReplace.length; i++) { | |
if (str_acento.indexOf(strToReplace.charAt(i)) != -1) { | |
nova += str_sem_acento.substr(str_acento.search(strToReplace.substr(i, 1)), 1); | |
} else { | |
nova += strToReplace.substr(i, 1); | |
} | |
} | |
return nova.toLowerCase().replace(/\s/g, '-').replace(/[\?\!\.\@\#\%\"\'\$]/g, ''); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment