601 B
601 B
title, tags, firstSeen, lastUpdated
| title | tags | firstSeen | lastUpdated |
|---|---|---|---|
| slugify | string,regexp,intermediate | 2020-10-04T09:45:43+03:00 | 2020-10-04T10:36:38+03:00 |
Converts a string to a URL-friendly slug.
- Use
String.prototype.toLowerCase()andString.prototype.trim()to normalize the string. - Use
String.prototype.replace()to replace spaces, dashes and underscores with-and remove special characters.
const slugify = str =>
str
.toLowerCase()
.trim()
.replace(/[^\w\s-]/g, '')
.replace(/[\s_-]+/g, '-')
.replace(/^-+|-+$/g, '');
slugify('Hello World!'); // 'hello-world'