Prepare repository for merge

This commit is contained in:
Angelos Chalaris
2023-05-01 22:35:56 +03:00
parent fc4e61e6fa
commit b3ad01863a
578 changed files with 0 additions and 0 deletions

View File

@ -0,0 +1,26 @@
---
title: String to slug
type: snippet
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'
```