Skip to content

Instantly share code, notes, and snippets.

@josephdburdick
Created September 16, 2023 21:00
Show Gist options
  • Save josephdburdick/8b0cbf6583598590e5701327be8b41f2 to your computer and use it in GitHub Desktop.
Save josephdburdick/8b0cbf6583598590e5701327be8b41f2 to your computer and use it in GitHub Desktop.
Generate slug from string
function generateSlug(str: string): string {
return str
.trim() // Remove leading/trailing whitespace
.toLowerCase() // Convert to lowercase
.replace(/[^a-zA-Z0-9\s]/g, "") // Remove non-alphanumeric characters
.replace(/\s+/g, "-") // Replace spaces with hyphens
.replace(/-+/g, "-") // Replace consecutive hyphens with a single hyphen
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment