Skip to content

Instantly share code, notes, and snippets.

@vjnrv
Created January 30, 2012 12:31
Show Gist options
  • Save vjnrv/1704160 to your computer and use it in GitHub Desktop.
Save vjnrv/1704160 to your computer and use it in GitHub Desktop.
strtr no Javascript
/**
* Limpa as caracteres especiais da string atual
*
* @return {string}
*/
String.prototype.clean = function ()
{
return this.strtr(
'ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûýýþÿŔŕ´`"\'~^',
'AAAAAAACEEEEIIIIDNOOOOOOUUUUYbsaaaaaaaceeeeiiiidnoooooouuuyybyRr'
);
};
/**
* 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