Dev HelpersOpen library
← Back

Create a URL slug

Create a URL slug. A self-contained example using the JavaScript runtime.

Open this example in the interactive library

Source code

const slug = (s) =>
  s
    .normalize("NFD")
    .replace(/[\u0300-\u036f]/g, "")
    .toLowerCase()
    .trim()
    .replace(/[^a-z0-9]+/g, "-")
    .replace(/^-|-$/g, "");
console.log(slug("  Café & Code!  "));

Sample output

cafe-code