Files
30-seconds-of-code/snippets/slugify.md
2020-10-04 08:45:43 +02:00

28 lines
578 B
Markdown

---
title: slugify
tags: function,intermediate
---
Convert a string to a URL-friendly slug.
- Make the string lowercase and remove leading or trailing whitespace.
- Remove all characters that are not words, whitespace or hyphens.
- Replace whitespace with a single hyphen (`-`).
- Remove any leading or trailing hyphens.
```js
const slugify = (string) => {
const slug = string
.toLowerCase()
.trim()
.replace(/[^\w\s-]/g, '')
.replace(/[\s_-]+/g, '-')
.replace(/^-+|-+$/g, '');
return slug;
};
```
```js
slugify('Hello World!'); // 'hello-world'
```