Skip to content

Instantly share code, notes, and snippets.

@davidep87
Last active June 17, 2017 18:15
Show Gist options
  • Save davidep87/232d58395671ecad79dc5673f451f783 to your computer and use it in GitHub Desktop.
Save davidep87/232d58395671ecad79dc5673f451f783 to your computer and use it in GitHub Desktop.
return slug from a string
function slugify(text) {
return text.toString().toLowerCase()
.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(/\s+/g, '-') // Replace spaces with -
.replace(/[^\w\-]+/g, '') // Remove all non-word chars
.replace(/\-\-+/g, '-') // Replace multiple - with single -
.replace(/^-+/, '') // Trim - from start of text
.replace(/-+$/, '') // Trim - from end of text
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment