Skip to content

Instantly share code, notes, and snippets.

@xuannghia
Last active March 29, 2023 06:46
Show Gist options
  • Save xuannghia/ad28ad6b42e88fb5facd9a3b11cf2da2 to your computer and use it in GitHub Desktop.
Save xuannghia/ad28ad6b42e88fb5facd9a3b11cf2da2 to your computer and use it in GitHub Desktop.
Xóa dấu tiếng Việt để làm slug
/**
* Slugify a string
* @param {string} str
* @param {string=} separator
* @returns {string}
*/
export const slugify = (str, separator = '-') => {
return str
.normalize('NFD')
.replace(/[\u0300-\u036f]/g, '')
.toLowerCase()
.replace(/đ/g, 'd')
.replace(/[^a-z0-9-]/g, '-')
.replace(/-+/g, '-')
.replace(/^-|-$/g, '')
.replace(/-/g, separator);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment