Skip to content

Instantly share code, notes, and snippets.

@kynatro
Last active Jan 1, 2016
Embed
What would you like to do?
Slugify phrases/camel-case
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