Skip to content

Instantly share code, notes, and snippets.

@leonelsr
Created May 21, 2012 23:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leonelsr/2765447 to your computer and use it in GitHub Desktop.
Save leonelsr/2765447 to your computer and use it in GitHub Desktop.
String prototype that transforms any string into a "ID" usable string by replacing portuguese special characters into their simple equivalents and stripping all remaining special chars.
/* Transforms any string into a "ID" usable string by replacing portuguese special characters into their simple equivalents and stripping all remaining special chars. */
String.prototype.IDize = function () {
var texto = this;
var chrEspeciais = new Array("\u00e1", "\u00e0", "\u00e2", "\u00e3", "\u00e4", "\u00e9", "\u00e8", "\u00ea", "\u00eb", "\u00ed", "\u00ec", "\u00ee", "\u00ef", "\u00f3", "\u00f2", "\u00f4", "\u00f5", "\u00f6", "\u00fa", "\u00f9", "\u00fb", "\u00fc", "\u00e7", "\u00c1", "\u00c0", "\u00c2", "\u00c3", "\u00c4", "\u00c9", "\u00c8", "\u00ca", "\u00cb", "\u00cd", "\u00cc", "\u00ce", "\u00cf", "\u00d3", "\u00d2", "\u00d4", "\u00d5", "\u00d6", "\u00da", "\u00d9", "\u00db", "\u00dc", "\u00c7");
var chrNormais = new Array("a", "a", "a", "a", "a", "e", "e", "e", "e", "i", "i", "i", "i", "o", "o", "o", "o", "o", "u", "u", "u", "u", "c", "A", "A", "A", "A", "A", "E", "E", "E", "E", "I", "I", "I", "I", "O", "O", "O", "O", "O", "U", "U", "U", "U", "C");
for(index in chrEspeciais) {
texto = texto.replace(new RegExp(chrEspeciais[index], 'g'), chrNormais[index])
}
texto = texto.replace(/[^a-zA-Z0-9_-]/g, '');
return texto.toLowerCase();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment