Nest all content into snippets

This commit is contained in:
Angelos Chalaris
2023-05-07 16:07:29 +03:00
parent 2ecadbada9
commit 6a45d2ec07
1240 changed files with 0 additions and 0 deletions

27
snippets/js/s/slugify.md Normal file
View File

@ -0,0 +1,27 @@
---
title: String to slug
type: snippet
language: javascript
tags: [string,regexp]
cover: houses-rock-sea
dateModified: 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'
```