Created
April 12, 2026 07:55
-
-
Save mrhammadasif/92e539b76d1a037c7aa7bee83d8df880 to your computer and use it in GitHub Desktop.
Slugify the Text
This file contains hidden or 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 REGEX_SPACE = /\s+/g | |
| const REGEX_NONWHITESPACE = /[^\w\-]+/g | |
| const REGEX_DOUBLEDASH = /\-\-+/g | |
| const REGEX_NOTADASH = /^-+/ | |
| const REGEX_DASHWITHCHAR = /-+$/ | |
| export function slugify(text: string): string { | |
| return text | |
| .toString() | |
| .toLowerCase() | |
| .replace(REGEX_SPACE, '-') | |
| .replace(REGEX_NONWHITESPACE, '') | |
| .replace(REGEX_DOUBLEDASH, '-') | |
| .replace(REGEX_NOTADASH, '') | |
| .replace(REGEX_DASHWITHCHAR, '') | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment