Skip to content

Instantly share code, notes, and snippets.

@franklinjavier
Last active August 29, 2015 14:06
Show Gist options
  • Select an option

  • Save franklinjavier/2e6f56453b4736905bfd to your computer and use it in GitHub Desktop.

Select an option

Save franklinjavier/2e6f56453b4736905bfd to your computer and use it in GitHub Desktop.
/*
* Remove acentos
*
* @param {string} str - String a ser alterada
* @example removeAccent('São Paulo'); // Sao Paulo
*/
function removeAccent() {
var st = arguments[0].toLowerCase();
st = st.replace(/[\u00C0-\u00C5]/ig, 'a')
.replace(/[\u00C8-\u00CB]/ig, 'e')
.replace(/[\u00CC-\u00CF]/ig, 'i')
.replace(/[\u00D2-\u00D6]/ig, 'o')
.replace(/[\u00D9-\u00DC]/ig, 'u')
.replace(/[\-]{2}/g, '');
return ( st.replace(/[^a-z\- ]*/gi, '') );
}
/*
* Remove acentos e substitui espacos por hifen
*
* @param {string} str - String a ser alterada
* @example slugify('São Paulo'); // sao-paulo
*/
function slugify( str ) {
str = str
.toLowerCase()
.replace(/^\s+|\s+$/g, '')
.replace(/[\u00C0-\u00C5]/ig, 'a')
.replace(/[\u00C8-\u00CB]/ig, 'e')
.replace(/[\u00CC-\u00CF]/ig, 'i')
.replace(/[\u00D2-\u00D6]/ig, 'o')
.replace(/[\u00D9-\u00DC]/ig, 'u')
.replace(/[\u00D1]/ig, 'n')
.replace(/[^a-z0-9 ]+/gi, '')
.replace(/ /g, '-')
.replace(/[\-]{2}/g, '-')
.replace(/[^a-z0-9\- ]*/gi, '')
return str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment