Skip to content

Instantly share code, notes, and snippets.

@laocoi
Last active August 26, 2025 16:07
Show Gist options
  • Select an option

  • Save laocoi/45ce064205b2b97b2548476c4bae9d88 to your computer and use it in GitHub Desktop.

Select an option

Save laocoi/45ce064205b2b97b2548476c4bae9d88 to your computer and use it in GitHub Desktop.
Create Vietnamese slug from string - Javascript
function slugify(string){
const a = 'àáäâãåăæąçćčđďèéěėëêęğǵḧìíïîįłḿǹńňñòóöôœøṕŕřßşśšșťțùúüûǘůűūųẃẍÿýźžż·/_,:;'
const b = 'aaaaaaaaacccddeeeeeeegghiiiiilmnnnnooooooprrsssssttuuuuuuuuuwxyyzzz------'
const p = new RegExp(a.split('').join('|'), 'g')
return string.toString().toLowerCase()
.replace(/á|à|||ã|ă||||||â|||||/gi, 'a')
.replace(/é|è||||ê|ế||||/gi, 'e')
.replace(/i|í|ì||ĩ|/gi, 'i')
.replace(/ó|ò||õ||ô||||||ơ|||||/gi, 'o')
.replace(/ú|ù||ũ||ư|||||/gi, 'u')
.replace(/ý||||/gi, 'y')
.replace(/đ/gi, 'd')
.replace(/\s+/g, '-')
.replace(p, c => b.charAt(a.indexOf(c)))
.replace(/&/g, '-and-')
.replace(/[^\w\-]+/g, '')
.replace(/\-\-+/g, '-')
.replace(/^-+/, '')
.replace(/-+$/, '')
}
@le-huy-jh
Copy link

Thanks

@hantv-work
Copy link

Useful

@onbao165
Copy link

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment