Created
September 16, 2023 21:00
-
-
Save josephdburdick/8b0cbf6583598590e5701327be8b41f2 to your computer and use it in GitHub Desktop.
Generate slug from string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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