Files
30-seconds-of-code/snippets/slugify.md
Isabelle Viktoria Maciohsek 27c168ce55 Bake date into snippets
2021-06-13 13:55:00 +03:00

26 lines
601 B
Markdown

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