Skip to content

Instantly share code, notes, and snippets.

@adrianolsk
Forked from jesobreira/makeslug.js
Last active August 29, 2015 14:19
Show Gist options
  • Save adrianolsk/666c57a795cc7680cff1 to your computer and use it in GitHub Desktop.
Save adrianolsk/666c57a795cc7680cff1 to your computer and use it in GitHub Desktop.
/*
* Javascript makeslug()
* by J. Santos <jefrey[at]jefrey[dot]ml>
*/
/*
Usage:
string makeslug( string val [, string replaceBy = "-" ] )
Example:
makeslug(" This is a string fuLL of áccêntèd and InVaLid (?) cHaRs!!! ");
Will return:
this-is-a-string-full-of-accented-and-invalid-chars
And:
makeslug(" This is a string fuLL of áccêntèd and InVaLid (?) cHaRs!!! ", "_");
Will return:
this_is_a_string_full_of_accented_and_invalid_chars
*/
function makeslug(val, replaceBy) {
replaceBy = replaceBy || '-';
var mapaAcentosHex = { // by @marioluan and @lelotnk
a : /[\xE0-\xE6]/g,
A : /[\xC0-\xC6]/g,
e : /[\xE8-\xEB]/g, // if you're gonna echo this
E : /[\xC8-\xCB]/g, // JS code through PHP, do
i : /[\xEC-\xEF]/g, // not forget to escape these
I : /[\xCC-\xCF]/g, // backslashes (\), by repeating
o : /[\xF2-\xF6]/g, // them (\\)
O : /[\xD2-\xD6]/g,
u : /[\xF9-\xFC]/g,
U : /[\xD9-\xDC]/g,
c : /\xE7/g,
C : /\xC7/g,
n : /\xF1/g,
N : /\xD1/g,
};
for ( var letra in mapaAcentosHex ) {
var expressaoRegular = mapaAcentosHex[letra];
val = val.replace( expressaoRegular, letra );
}
val = val.toLowerCase();
val = val.replace(/[^a-z0-9\-]/g, " ");
do {
val = val.replace(' ', ' ');
} while (val.indexOf(' ')>-1);
val = val.trim();
val = val.replace(/\s/g, replaceBy);
return val;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment