Created
January 15, 2024 02:38
-
-
Save myagizmaktav/c143c831753079b5fac1fa69f6cc8e89 to your computer and use it in GitHub Desktop.
Convert to non english chars to english
This file contains hidden or 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
| // https://byby.dev/js-slugify-string | |
| function slugify(str) { | |
| return String(str) | |
| .normalize('NFKD') // split accented characters into their base characters and diacritical marks | |
| .replace(/[\u0300-\u036f]/g, '') // remove all the accents, which happen to be all in the \u03xx UNICODE block. | |
| .trim() // trim leading or trailing whitespace | |
| .toLowerCase() // convert to lowercase | |
| .replace(/[^a-z0-9 -]/g, '') // remove non-alphanumeric characters | |
| .replace(/\s+/g, '-') // replace spaces with hyphens | |
| .replace(/-+/g, '-'); // remove consecutive hyphens | |
| } | |
| console.log(slugify("The Quick Brown Fox Jumps Over The Lazy Dog! ")) | |
| // "the-quick-brown-fox-jumps-over-the-lazy-dog" | |
| console.log(slugify("söme stüff with áccènts")) | |
| // "some-stuff-with-accents" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment