Created
July 7, 2023 14:00
-
-
Save solrevdev/91f05dd41e2d9674a080f926ec8155a7 to your computer and use it in GitHub Desktop.
vanilla javascript method to slugify a string
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
function slugify(input) { | |
if (!input) | |
return ''; | |
// make lower case and trim | |
var slug = input.toLowerCase().trim(); | |
// remove accents from charaters | |
slug = slug.normalize('NFD').replace(/[\u0300-\u036f]/g, '') | |
// replace invalid chars with spaces | |
slug = slug.replace(/[^a-z0-9\s-]/g, ' ').trim(); | |
// replace multiple spaces or hyphens with a single hyphen | |
slug = slug.replace(/[\s-]+/g, '-'); | |
return slug; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment