Created
January 30, 2012 12:31
-
-
Save vjnrv/1704160 to your computer and use it in GitHub Desktop.
strtr no Javascript
This file contains 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
/** | |
* Limpa as caracteres especiais da string atual | |
* | |
* @return {string} | |
*/ | |
String.prototype.clean = function () | |
{ | |
return this.strtr( | |
'ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûýýþÿŔŕ´`"\'~^', | |
'AAAAAAACEEEEIIIIDNOOOOOOUUUUYbsaaaaaaaceeeeiiiidnoooooouuuyybyRr' | |
); | |
}; |
This file contains 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
/** | |
* Traduz as caracteres de 'from' para 'to' | |
* | |
* @param {string} from | |
* @param {string} to | |
* @return {string} | |
*/ | |
String.prototype.strtr = function (from, to) | |
{ | |
var i, | |
_this = this.toString(); | |
from = (from + '').split(''); | |
to = (to + '').split(''); | |
i = from.length; | |
while (i--) | |
{ | |
if (_this.match(from[i])) { | |
// Troca por to, Se to[i] não existir a nova char será vazia | |
_this = _this.replace(new RegExp('\\' + from[i], 'ig'), (to[i] || '')); | |
} | |
} | |
return _this; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment