Skip to content

Instantly share code, notes, and snippets.

@Jezfx
Created February 2, 2017 12:31
Show Gist options
  • Save Jezfx/b7560c3c77dac73ac8c22d67ad086f0f to your computer and use it in GitHub Desktop.
Save Jezfx/b7560c3c77dac73ac8c22d67ad086f0f to your computer and use it in GitHub Desktop.
slugify strings
export function slugify(text) {
return text.toString().toLowerCase()
.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