Skip to content

Instantly share code, notes, and snippets.

@kcjpop
Last active May 24, 2017 19:55
Show Gist options
  • Save kcjpop/10585530 to your computer and use it in GitHub Desktop.
Save kcjpop/10585530 to your computer and use it in GitHub Desktop.
Vietnamese slugify
const slugify = str => str
.trim()
.toLowerCase()
.replace(/(á|à|ả|ã|ạ|â|ấ|ầ|ẩ|ậ|ă|ằ|ắ|ặ|ẳ|ẵ)/g, 'a')
.replace(/(é|è|ẻ|ẽ|ẹ|ê|ế|ề|ể|ễ|ệ)/g, 'e')
.replace(/(ó|ò|ỏ|õ|ọ|ơ|ờ|ớ|ở|ợ|ỡ|ô|ố|ồ|ổ|ộ)/g, 'o')
.replace(/(í|ì|ỉ|ĩ|ị)/g, 'i')
.replace(/(ú|ù|ủ|ũ|ụ|ư|ứ|ừ|ử|ự)/g, 'u')
.replace(/(ý|ỳ|ỷ|ỹ|ỵ)/g, 'y')
.replace(/(đ)/g, 'd')
.replace(/[^\w\s]/g, '') // Remove non-word characters
.replace(/(\s)+/g, '-') // Replace spaces with one hyphen
.replace(/-+/g, '-') // Remove duplicate hyphens
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment