Created
July 25, 2020 07:13
-
-
Save hasanuzzamanbe/666d4d67c11fa81411b1e62de31e5306 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function stringToSlug (str) { | |
str = str.replace(/^\s+|\s+$/g, ''); | |
str = str.toLowerCase(); | |
var from = 'àáäâèéëêìíïîòóöôùúüûñç·/_,:;'; | |
var to = 'aaaaeeeeiiiioooouuuunc------'; | |
for (var i = 0, l = from.length; i < l; i++) { | |
str = str.replace(new RegExp(from.charAt(i), 'g'), to.charAt(i)); | |
} | |
str = str.replace(/[^a-z0-9 -]/g, '') | |
.replace(/\s+/g, '-') | |
.replace(/-+/g, '-'); | |
return str; | |
} | |
// stringToSlug(Lorem Ipsum) expected output: lorem-ipsum |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment