JavaScript: Chuyển Tiếng Việt Có Dấu Sang Không Dấu - Gists · GitHub
Instantly share code, notes, and snippets.
- Download ZIP
- 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.
- 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; |
| } |
ThuanLCSE commented Mar 31, 2018
Cám ơn hu2di
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
bestinme122 commented Apr 13, 2018
Thanks dong chi nhieu
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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; }Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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"
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
mvhix9 commented Dec 5, 2018
Cảm ơn bạn nhiều
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
khoailang99 commented Feb 22, 2019
cảm ơn bạn!!!
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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]
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
huynguyen-hl commented Jul 15, 2019
cảm ơn nhiều 😁😁
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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 ĐAISorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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");Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
chellongCoder commented Aug 18, 2020
thanks @davistran86
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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!
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
hu2di commented Aug 27, 2020
Thank @hungdev and @tannguyenit.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
haile01 commented Mar 23, 2021
Truly appreciate your work <3
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
QuocDat269 commented Mar 30, 2021
thanks
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
gnoluv9x commented Aug 6, 2021
thanks alot
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
dev-panpic commented Feb 16, 2022
Great. Thank you so much
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
phananhtrung commented Mar 21, 2022
có vẻ đã hoàn thiện. cảm ơn bạn
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
longnguyen28297 commented May 10, 2022
Cảm ơn bạn
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
PainterHalver commented May 24, 2022
Cảm ơn bạn nhiều
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
minhquyn1204 commented Jul 5, 2022
cảm ơn tiền bối
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
lenhannhan123 commented Aug 23, 2022
cảm ơn bạn
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Mrcryptonguyen commented Sep 21, 2022
gid:m9Jk3xmt8AiYwJAKhi5k89
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
gnoluv9x commented Mar 6, 2023
Tks for share :)
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
quocthinhle commented Apr 11, 2023
Ok nice
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
sylitas commented Jun 4, 2023
ngonnnnn thanks anh em
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
FE-Tuanxinhtrai commented Jun 14, 2023
thanks
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
Sorry, something went wrong.
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
-
Các Toán Tử Trong JavaScript
-
Ký Hiệu $ Trong JavaScript Là Gì?
-
Toán Tử (...) đã Thay đổi Javascript (P1) - Viblo
-
18+ Kĩ Thuật Rút Gọn Code Javascript Của Bạn - Viblo
-
4 Toán Tử JavaScript Với Dấu Chấm Hỏi - Trang Chủ - .vn
-
Cách Sử Dụng Chuỗi (String) Trong JavaScript - Web Cơ Bản
-
Bài 3: Toán Tử Và Quy Tắc Dấu Ngoặc Trong Javascript
-
Dấu Ba Chấm Trong JavaScript
-
Loại Bỏ Dấu Tiếng Việt Trong Chuỗi Javascript ES6 - Tunglt
-
Cách Xử Lý Chuỗi Trong Javascript
-
Top 15 Dấu Khác Trong Js
-
Expressions And Operators - JavaScript - MDN Web Docs
-
So Sánh 3 Dấu Bằng Trong Javascript
-
Dấu $ Trong Jquery Là Gì, Nó Có ý Nghĩa Như Thế Nào? Ngoài Ra, Làm ...
-
Lấy 2 Số Sau Dấu Phẩy Trong JavaScript
-
Dấu Chấm Phẩy Trong JavaScript - Tech Wiki