Skip to content

Instantly share code, notes, and snippets.

@dtw
Created May 23, 2022 14:46

Revisions

  1. dtw revised this gist May 23, 2022. No changes.
  2. dtw created this gist May 23, 2022.
    12 changes: 12 additions & 0 deletions slugify.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    function slugify(text) {
    // from https://gist.github.com/codeguy/6684588?permalink_comment_id=3243980#gistcomment-3243980
    return text
    .toString() // Cast to string (optional)
    .normalize('NFKD') // The normalize() using NFKD method returns the Unicode Normalization Form of a given string.
    .toLowerCase() // Convert the string to lowercase letters
    .trim() // Remove whitespace from both sides of a string (optional)
    .replace(/\s+/g, '-') // Replace spaces with -
    .replace(/[^\w\-]+/g, '') // Remove all non-word chars
    .replace(/\-\-+/g, '-') // Replace multiple - with single -
    .replace(/\-$/g, ''); // Remove trailing -
    }