feat: add slugify and use const in yesterday
This commit is contained in:
27
snippets/slugify.md
Normal file
27
snippets/slugify.md
Normal file
@ -0,0 +1,27 @@
|
||||
---
|
||||
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'
|
||||
```
|
||||
Reference in New Issue
Block a user