Skip to content

Instantly share code, notes, and snippets.

@bluzky
Last active April 8, 2024 05:01
Show Gist options
  • Star 25 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save bluzky/b8c205c98ff3318907b30c3e0da4bf3f to your computer and use it in GitHub Desktop.
Save bluzky/b8c205c98ff3318907b30c3e0da4bf3f to your computer and use it in GitHub Desktop.
Remove vietnamese accent javascript / Bỏ dấu tiếng Việt
function stringToSlug(str) {
// remove accents
var from = "àáãảạăằắẳẵặâầấẩẫậèéẻẽẹêềếểễệđùúủũụưừứửữựòóỏõọôồốổỗộơờớởỡợìíỉĩịäëïîöüûñçýỳỹỵỷ",
to = "aaaaaaaaaaaaaaaaaeeeeeeeeeeeduuuuuuuuuuuoooooooooooooooooiiiiiaeiiouuncyyyyy";
for (var i=0, l=from.length ; i < l ; i++) {
str = str.replace(RegExp(from[i], "gi"), to[i]);
}
str = str.toLowerCase()
.trim()
.replace(/[^a-z0-9\-]/g, '-')
.replace(/-+/g, '-');
return str;
}
@thuyetnv95
Copy link

thuyetnv95 commented May 30, 2020

You are missing y case

Ex: nước Mỹ

@bluzky
Copy link
Author

bluzky commented Jun 3, 2020

fixed. Thank you

@trungpq163
Copy link

<3

@Khanaru220
Copy link

Thank you anh, em adapt được logic của anh vào project của em. Em có thêm 1 gợi ý trong lúc em sử dụng:
Em chia "dấu tiếng Việt" thành 2 loại:

  1. tone mark: các thanh (sắc, huyền, hỏi, ngã, nặng)
  2. diacritical marks: dấu phụ (ă,â,ê,ô,ơ,ư,đ) tụi nó vẫn thuộc bảng chữ cái tiếng Việt. Case em dùng là để loop qua "pangrams tiếng Việt"
    • tiếng Anh: The quick brown fox jumps over the lazy dog
    • tiếng Việt: Trường ở quê sạch và đẹp lắm do bố xây kĩ

Nên code sẽ có 2 options: (0) xóa toàn bộ dấu; (1) chỉ xóa tone mark

function stringToSlug(str, option = 0) {
	var from, to;
  // remove all marks (Latin alphabet)
	if(option === 0){
		from = "àáãảạăằắẳẵặâầấẩẫậèéẻẽẹêềếểễệđùúủũụưừứửữựòóỏõọôồốổỗộơờớởỡợìíỉĩịäëïîöüûñçýỳỹỵỷ",
                to   = "aaaaaaaaaaaaaaaaaeeeeeeeeeeeduuuuuuuuuuuoooooooooooooooooiiiiiaeiiouuncyyyyy";
	}

  //remove tone mark (Vietnamese alphabet)
	if(option === 1){
		from = "àáãảạăằắẳẵặâầấẩẫậèéẻẽẹêềếểễệđùúủũụưừứửữựòóỏõọôồốổỗộơờớởỡợìíỉĩịäëïîöüûñçýỳỹỵỷ",
		to   = "aaaaaăăăăăăââââââeeeeeêêêêêêđuuuuuưưưưưưoooooôôôôôôơơơơơơiiiiiaeiiouuncyyyyy";
	}

@GiangThanhDat
Copy link

GiangThanhDat commented Sep 13, 2022

Thanks for sharing <3

@hhuuthien
Copy link

You saved my life!!! Thanks a lot

@southxzx
Copy link

God job!

@DinhNghiaVo
Copy link

it not working for "Đ"

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