Last active
April 16, 2024 20:52
-
-
Save abdelfattahradwan/10dc8d5fcb90b949cdc01d15fd0dc707 to your computer and use it in GitHub Desktop.
A function that converts strings into URL-friendly slugs by lowercasing, replacing non-alphanumeric characters with dashes, and trimming leading/trailing dashes.
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
const NON_ALPHANUMERIC_REGEX = /[^a-z0-9]+/g; | |
const LEADING_TRAILING_DASHES_REGEX = /^-+|-+$/g; | |
export default function slugify(title: string): string { | |
return title | |
.toLowerCase() | |
.replace(NON_ALPHANUMERIC_REGEX, "-") | |
.replace(LEADING_TRAILING_DASHES_REGEX, ""); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment