Skip to content

Instantly share code, notes, and snippets.

@myagizmaktav
Created January 15, 2024 02:38
Show Gist options
  • Select an option

  • Save myagizmaktav/c143c831753079b5fac1fa69f6cc8e89 to your computer and use it in GitHub Desktop.

Select an option

Save myagizmaktav/c143c831753079b5fac1fa69f6cc8e89 to your computer and use it in GitHub Desktop.
Convert to non english chars to english
// 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