Skip to content

Instantly share code, notes, and snippets.

@craigerskine
Last active December 8, 2023 17:34
Show Gist options
  • Save craigerskine/3a984c8fdc3a40dfed3051394195c2f3 to your computer and use it in GitHub Desktop.
Save craigerskine/3a984c8fdc3a40dfed3051394195c2f3 to your computer and use it in GitHub Desktop.
slugify(text) {
return text?.toString()
.toLowerCase()
.trim()
.replace(/\.+/g, '-') // . to - (this can be removed - my specific usecase)
.replace(/&/g, '-and-') // & to -and- (this can be removed - my specific usecase)
.normalize('NFKD') // unicode normalization
.replace(/[^\w\-]+/g, '') // remove other crazy chars
.replace(/\s+/g, '-') // space to -
.replace(/\-\-+/g, '-') // multiple - to single -
.replace(/^-+/, "") // remove - from start
.replace(/-+$/, "") // remove - from end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment