JavaScript: Chuyển Tiếng Việt Có Dấu Sang Không Dấu - Gists · GitHub

Skip to content Search Gists Search Gists All gists Back to GitHub Sign in Sign up Sign in Sign up Dismiss alert {{ message }}

Instantly share code, notes, and snippets.

@hu2di hu2di/ConvertVie.js Last active March 1, 2025 13:15 Show Gist options
  • Star (114) You must be signed in to star a gist
  • Fork (29) You must be signed in to fork a gist
  • Embed Select an option
    • Embed Embed this gist in your website.
    • Share Copy sharable link for this gist.
    • Clone via HTTPS Clone using the web URL.

    No results found

    Learn more about clone URLs Clone this repository at <script src="https://gist.github.com/hu2di/e80d99051529dbaa7252922baafd40e3.js"></script>
  • Save hu2di/e80d99051529dbaa7252922baafd40e3 to your computer and use it in GitHub Desktop.
Code Revisions 3 Stars 114 Forks 29 Embed Select an option
  • Embed Embed this gist in your website.
  • Share Copy sharable link for this gist.
  • Clone via HTTPS Clone using the web URL.

No results found

Learn more about clone URLs Clone this repository at <script src="https://gist.github.com/hu2di/e80d99051529dbaa7252922baafd40e3.js"></script> Save hu2di/e80d99051529dbaa7252922baafd40e3 to your computer and use it in GitHub Desktop. Download ZIP JavaScript: Chuyển tiếng Việt có dấu sang không dấu Raw ConvertVie.js 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 Show hidden characters
function removeVietnameseTones(str) {
str = str.replace(/à|á|||ã|â||||||ă|||||/g,"a");
str = str.replace(/è|é||||ê||ế|||/g,"e");
str = str.replace(/ì|í|||ĩ/g,"i");
str = str.replace(/ò|ó|||õ|ô||||||ơ|||||/g,"o");
str = str.replace(/ù|ú|||ũ|ư|||||/g,"u");
str = str.replace(/|ý|||/g,"y");
str = str.replace(/đ/g,"d");
str = str.replace(/À|Á|||Ã|Â||||||Ă|||||/g, "A");
str = str.replace(/È|É||||Ê|||||/g, "E");
str = str.replace(/Ì|Í|||Ĩ/g, "I");
str = str.replace(/Ò|Ó|||Õ|Ô||||||Ơ|||||/g, "O");
str = str.replace(/Ù|Ú|||Ũ|Ư|||||/g, "U");
str = str.replace(/|Ý|||/g, "Y");
str = str.replace(/Đ/g, "D");
// Some system encode vietnamese combining accent as individual utf-8 characters
// Một vài bộ encode coi các dấu mũ, dấu chữ như một kí tự riêng biệt nên thêm hai dòng này
str = str.replace(/\u0300|\u0301|\u0303|\u0309|\u0323/g, ""); // ̀ ́ ̃ ̉ ̣ huyền, sắc, ngã, hỏi, nặng
str = str.replace(/\u02C6|\u0306|\u031B/g, ""); // ˆ ̆ ̛ Â, Ê, Ă, Ơ, Ư
// Remove extra spaces
// Bỏ các khoảng trắng liền nhau
str = str.replace(/+/g," ");
str = str.trim();
// Remove punctuations
// Bỏ dấu câu, kí tự đặc biệt
str = str.replace(/!|@|%|\^|\*|\(|\)|\+|\=|\<|\>|\?|\/|,|\.|\:|\;|\'|\"|\&|\#|\[|\]|~|\$|_|`|-|{|}|\||\\/g," ");
return str;
}
Load earlier comments... @ThuanLCSE Copy link

ThuanLCSE commented Mar 31, 2018

Cám ơn hu2di

Uh oh!

There was an error while loading. Please reload this page.

@bestinme122 Copy link

bestinme122 commented Apr 13, 2018

Thanks dong chi nhieu

Uh oh!

There was an error while loading. Please reload this page.

@hungdev Copy link

hungdev commented Jun 8, 2018 edited Loading

Uh oh!

There was an error while loading. Please reload this page.

thiếu ký tự in hoa

function xoa_dau(str) { str = str.replace(/à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ/g, "a"); str = str.replace(/è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ/g, "e"); str = str.replace(/ì|í|ị|ỉ|ĩ/g, "i"); str = str.replace(/ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ/g, "o"); str = str.replace(/ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ/g, "u"); str = str.replace(/ỳ|ý|ỵ|ỷ|ỹ/g, "y"); str = str.replace(/đ/g, "d"); str = str.replace(/À|Á|Ạ|Ả|Ã|Â|Ầ|Ấ|Ậ|Ẩ|Ẫ|Ă|Ằ|Ắ|Ặ|Ẳ|Ẵ/g, "A"); str = str.replace(/È|É|Ẹ|Ẻ|Ẽ|Ê|Ề|Ế|Ệ|Ể|Ễ/g, "E"); str = str.replace(/Ì|Í|Ị|Ỉ|Ĩ/g, "I"); str = str.replace(/Ò|Ó|Ọ|Ỏ|Õ|Ô|Ồ|Ố|Ộ|Ổ|Ỗ|Ơ|Ờ|Ớ|Ợ|Ở|Ỡ/g, "O"); str = str.replace(/Ù|Ú|Ụ|Ủ|Ũ|Ư|Ừ|Ứ|Ự|Ử|Ữ/g, "U"); str = str.replace(/Ỳ|Ý|Ỵ|Ỷ|Ỹ/g, "Y"); str = str.replace(/Đ/g, "D"); return str; }

Uh oh!

There was an error while loading. Please reload this page.

@hakicode Copy link

hakicode commented Nov 29, 2018

"PHẦN MỘT. LỊCH SỬ THẾ GIỚI THỜI NGUYÊN THỦY, CỔ ĐẠI VÀ TRUNG ĐẠI" Không convert được chữ "thủy"

Uh oh!

There was an error while loading. Please reload this page.

@mvhix9 Copy link

mvhix9 commented Dec 5, 2018

Cảm ơn bạn nhiều

Uh oh!

There was an error while loading. Please reload this page.

@khoailang99 Copy link

khoailang99 commented Feb 22, 2019

cảm ơn bạn!!!

Uh oh!

There was an error while loading. Please reload this page.

@alang-dev Copy link

alang-dev commented Apr 10, 2019

"PHẦN MỘT. LỊCH SỬ THẾ GIỚI THỜI NGUYÊN THỦY, CỔ ĐẠI VÀ TRUNG ĐẠI" Không convert được chữ "thủy". Chữ "thủy" không convert được là do dấu hỏi nó là một ký tự riêng, nếu chạy loop bạn sẽ thấy "thủy" -> [t, h, u, "dấu hỏi", y]

Uh oh!

There was an error while loading. Please reload this page.

@huynguyen-hl Copy link

huynguyen-hl commented Jul 15, 2019

cảm ơn nhiều 😁😁

Uh oh!

There was an error while loading. Please reload this page.

@lazidoca Copy link

lazidoca commented Jul 23, 2019 edited Loading

Uh oh!

There was an error while loading. Please reload this page.

1 cách khác ngắn gọn hơn với ES2015/ES6 String.Prototype.Normalize()

const str = "PHẦN MỘT. LỊCH SỬ THẾ GIỚI THỜI NGUYÊN THỦY, CỔ ĐẠI VÀ TRUNG ĐẠI"; const newStr = str.normalize("NFD").replace(/[\u0300-\u036f]/g, ""); console.log(newStr) >> PHAN MOT. LICH SU THE GIOI THOI NGUYEN THUY, CO ĐAI VA TRUNG ĐAI

Uh oh!

There was an error while loading. Please reload this page.

@davistran86 Copy link

davistran86 commented Jul 29, 2019 edited Loading

Uh oh!

There was an error while loading. Please reload this page.

@lazidoca: chữ Đ vẫn bị bạn ơi. Chuyển thành kiểu này thì xử luôn chữ Đ:

str .normalize("NFD") .replace(/[\u0300-\u036f]/g, "") .replace(/đ/g, "d") .replace(/Đ/g, "D");

Uh oh!

There was an error while loading. Please reload this page.

@chellongCoder Copy link

chellongCoder commented Aug 18, 2020

thanks @davistran86

Uh oh!

There was an error while loading. Please reload this page.

@tannguyenit Copy link

tannguyenit commented Aug 26, 2020 edited Loading

Uh oh!

There was an error while loading. Please reload this page.

function convertViToEn(str, toUpperCase = false) { str = str.toLowerCase(); str = str.replace(/à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ/g, "a"); str = str.replace(/è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ/g, "e"); str = str.replace(/ì|í|ị|ỉ|ĩ/g, "i"); str = str.replace(/ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ/g, "o"); str = str.replace(/ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ/g, "u"); str = str.replace(/ỳ|ý|ỵ|ỷ|ỹ/g, "y"); str = str.replace(/đ/g, "d"); // Some system encode vietnamese combining accent as individual utf-8 characters str = str.replace(/\u0300|\u0301|\u0303|\u0309|\u0323/g, ""); // Huyền sắc hỏi ngã nặng str = str.replace(/\u02C6|\u0306|\u031B/g, ""); // Â, Ê, Ă, Ơ, Ư return toUpperCase ? str.toUpperCase() : str; }

Pls try this!

Uh oh!

There was an error while loading. Please reload this page.

@hu2di Copy link Author

hu2di commented Aug 27, 2020

Thank @hungdev and @tannguyenit.

Uh oh!

There was an error while loading. Please reload this page.

@haile01 Copy link

haile01 commented Mar 23, 2021

Truly appreciate your work <3

Uh oh!

There was an error while loading. Please reload this page.

@QuocDat269 Copy link

QuocDat269 commented Mar 30, 2021

thanks

Uh oh!

There was an error while loading. Please reload this page.

@gnoluv9x Copy link

gnoluv9x commented Aug 6, 2021

thanks alot

Uh oh!

There was an error while loading. Please reload this page.

@dev-panpic Copy link

dev-panpic commented Feb 16, 2022

Great. Thank you so much

Uh oh!

There was an error while loading. Please reload this page.

@phananhtrung Copy link

phananhtrung commented Mar 21, 2022

có vẻ đã hoàn thiện. cảm ơn bạn

Uh oh!

There was an error while loading. Please reload this page.

@longnguyen28297 Copy link

longnguyen28297 commented May 10, 2022

Cảm ơn bạn

Uh oh!

There was an error while loading. Please reload this page.

@PainterHalver Copy link

PainterHalver commented May 24, 2022

Cảm ơn bạn nhiều

Uh oh!

There was an error while loading. Please reload this page.

@minhquyn1204 Copy link

minhquyn1204 commented Jul 5, 2022

cảm ơn tiền bối

Uh oh!

There was an error while loading. Please reload this page.

@lenhannhan123 Copy link

lenhannhan123 commented Aug 23, 2022

cảm ơn bạn

Uh oh!

There was an error while loading. Please reload this page.

@Mrcryptonguyen Copy link

Mrcryptonguyen commented Sep 21, 2022

gid:m9Jk3xmt8AiYwJAKhi5k89

Uh oh!

There was an error while loading. Please reload this page.

@gnoluv9x Copy link

gnoluv9x commented Mar 6, 2023

Tks for share :)

Uh oh!

There was an error while loading. Please reload this page.

@quocthinhle Copy link

quocthinhle commented Apr 11, 2023

Ok nice

Uh oh!

There was an error while loading. Please reload this page.

@sylitas Copy link

sylitas commented Jun 4, 2023

ngonnnnn thanks anh em

Uh oh!

There was an error while loading. Please reload this page.

@FE-Tuanxinhtrai Copy link

FE-Tuanxinhtrai commented Jun 14, 2023

thanks

Uh oh!

There was an error while loading. Please reload this page.

@hanti1999 Copy link

hanti1999 commented Jul 6, 2023 edited Loading

Uh oh!

There was an error while loading. Please reload this page.

Cảm ơn bạn, mình cần thêm dấu gạch nối nữa nên đã tìm được cái này: /* str = str.replace(/\W+/g, ' '); str = str.replace(/\s/g, '-'); */ Thêm 2 cái này ở cuối để chuyển khoảng trắng thành dấu gạch nối ví dụ: Viet Nam => Viet-Nam

Uh oh!

There was an error while loading. Please reload this page.

@vuduynhiennn Copy link

vuduynhiennn commented Jul 12, 2023

cảm ơn bạn nhiều nha trông không tối ưu lắm nhưng lúc chạy thấy ra kết quả ok

Uh oh!

There was an error while loading. Please reload this page.

@cunghayday Copy link

cunghayday commented Oct 3, 2023 edited Loading

Uh oh!

There was an error while loading. Please reload this page.

Xin cảm ơn bạn, đúng thứ tôi dang cần. Nhưng tôi thấy bạn nên để phần xoá nhiều khoảng trắng sau phần xoá ký tự đặc biệt, bởi vì sau khi xoá ký tự đặc biệt đôi khi lại xuất hiện trường hợp 2 khoảng trắng liền kề. Xoá ký tự đặc biệt theo tôi nên dùng str = str.replace(/[^a-zA-Z0-9 ]/g, ""); thì đỡ phức tạp hơn.

Uh oh!

There was an error while loading. Please reload this page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment You can’t perform that action at this time.

Từ khóa » Dấu Js