Last active
May 7, 2019 02:09
-
-
Save davidnguyen11/5665a49bd16748215f625975565d8e62 to your computer and use it in GitHub Desktop.
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
function slugify(str) { | |
var newStr = str; | |
if (newStr) { | |
newStr = newStr.toLowerCase(); | |
newStr = newStr.replace(/̀|̣.|΄|̃|̉/g, ''); | |
newStr = newStr.replace(/à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ/g, 'a'); | |
newStr = newStr.replace(/è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ/g, 'e'); | |
newStr = newStr.replace(/ì|í|ị|ỉ|ĩ/g, 'i'); | |
newStr = newStr.replace(/ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ/g, 'o'); | |
newStr = newStr.replace(/ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ/g, 'u'); | |
newStr = newStr.replace(/ỳ|ý|ỵ|ỷ|ỹ/g, 'y'); | |
newStr = newStr.replace(/đ/g, 'd'); | |
newStr = newStr.replace(/!|@|%|\^|\*|\(|\)|\+|\=|\<|\>|\?|\/|,|\.|\:|\;|\'| |\"|\&|\#|\[|\]|~|$|_/g, '-'); | |
newStr = newStr.replace(/[^a-z0-9\-]/g, ''); | |
// thay thế 2- thành 1- | |
newStr = newStr.replace(/-+-/g, '-'); | |
newStr = newStr.replace(/^\-+|\-+$/g, ''); | |
return newStr; | |
} | |
return ''; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment