Skip to content

Instantly share code, notes, and snippets.

@kynatro
Last active January 1, 2016 13:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kynatro/8152642 to your computer and use it in GitHub Desktop.
Save kynatro/8152642 to your computer and use it in GitHub Desktop.
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