Slugify

2 parameters

6 of 6

function slugify(text, separator)

Converts a string to a URL-friendly slug

Parameters

Name
Type
Required
Description
separatorstringOptionalThe separator character to use
textstringRequiredThe text to convert to a slug

Function Implementation

function slugify(text, separator = '-') {
  return text
    .toString()
    .toLowerCase()
    .trim()
    .replace(/[^\w\s-]/g, '')
    .replace(/[\s_-]+/g, separator)
    .replace(/^-+|-+$/g, '');
}