Last active
January 1, 2016 13:39
-
-
Save kynatro/8152642 to your computer and use it in GitHub Desktop.
Slugify phrases/camel-case
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(str){ | |
return str.replace(/[^\w\d\s\-]+/g, "") // Strip invalid characters | |
.replace(/_+/g, "-") // Replace _ with - | |
.replace(/^[\s|\-]+|[\s|\-]+$/, "") // Trim whitespace | |
.replace(/(\s+)/g, ",") // Replace spaces with , for spliting | |
.replace(/([A-Z]+)/g, ",$1") // Add , between capitals for splitting | |
.replace(/^,/, "") // Trim off the first comma if one was added | |
.split(",") // Split it apart | |
.join("-") // Put it together | |
.replace(/-+/g, "-") // Get rid of serial - | |
.toLowerCase(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment