Câu chuyện này bắt đầu bằng một lời thú nhận. Tôi đã sợ Unicode trong một thời gian dài. Khi một tác vụ lập trình yêu cầu kiến thức về Unicode, tôi đã tìm kiếm một giải pháp có thể hack được cho vấn đề này mà không hiểu rõ mình đang làm gì
Sự lảng tránh của tôi tiếp tục cho đến khi tôi gặp phải một vấn đề đòi hỏi kiến thức chi tiết về Unicode. Không có cách nào để áp dụng các giải pháp tình thế
Sau khi nỗ lực, đọc một loạt bài báo — thật ngạc nhiên là nó không khó hiểu. Cái giếng. một số bài viết đã yêu cầu đọc ít nhất 3 lần
Hóa ra, Unicode là một tiêu chuẩn phổ quát và thanh lịch, nhưng nó có thể khó khăn vì nó hoạt động với một loạt các thuật ngữ trừu tượng.
Nếu bạn có lỗ hổng trong việc hiểu Unicode, bây giờ là thời điểm thích hợp để đối mặt với nó. Nó không phải là khó khăn. Pha cho mình một tách trà hoặc cà phê ngon ☕. Và hãy đi sâu vào thế giới tuyệt vời của sự trừu tượng, nhân vật, thiên thể và người thay thế
Bài viết giải thích các khái niệm cơ bản về Unicode, tạo nền tảng cần thiết
Sau đó, nó làm rõ cách JavaScript hoạt động với Unicode và những bẫy bạn có thể gặp phải
Bạn cũng sẽ học cách áp dụng các tính năng mới của ECMAScript 2015 để giải quyết một phần khó khăn
Sẳn sàng?
Mục lục
1. Ý tưởng đằng sau Unicode
Hãy bắt đầu với một câu hỏi đơn giản. Làm thế nào bạn có thể đọc và hiểu bài viết hiện tại? . bởi vì bạn biết ý nghĩa của các chữ cái và các từ như một nhóm các chữ cái
Tại sao bạn có thể hiểu ý nghĩa của các chữ cái? . bởi vì bạn [người đọc] và tôi [người viết] có một thỏa thuận về mối liên hệ giữa biểu tượng đồ họa [những gì nhìn thấy trên màn hình] và chữ cái tiếng Anh [ý nghĩa]
Điều tương tự cũng xảy ra với máy tính. Sự khác biệt là máy tính không hiểu ý nghĩa của các chữ cái. Đối với máy tính, các chữ cái chỉ là các chuỗi bit
Hãy tưởng tượng User1 gửi qua mạng một tin nhắn
0 cho User2javascript
console.log['cafe\u0301']; // => 'café'
console.log['café']; // => 'café'
Máy tính của người dùng1 không biết nghĩa của các chữ cái. Vì vậy, nó biến đổi
0 thành một dãy sốjavascript
console.log['cafe\u0301']; // => 'café'
console.log['café']; // => 'café'
2, trong đó mỗi chữ cái tương ứng duy nhất với một số.javascript
console.log['cafe\u0301']; // => 'café'
console.log['café']; // => 'café'
3 làjavascript
console.log['cafe\u0301']; // => 'café'
console.log['café']; // => 'café'
4,javascript
console.log['cafe\u0301']; // => 'café'
console.log['café']; // => 'café'
5 làjavascript
console.log['cafe\u0301']; // => 'café'
console.log['café']; // => 'café'
6, v.v. Những con số này được gửi đến máy tính của User2javascript
console.log['cafe\u0301']; // => 'café'
console.log['café']; // => 'café'
Khi máy tính của Người dùng 2 nhận được dãy số
2, nó sẽ sử dụng cùng một chữ cái để đánh số tương ứng và khôi phục thông báo. Sau đó, nó hiển thị thông báo chính xác.javascript
console.log['cafe\u0301']; // => 'café'
console.log['café']; // => 'café'
0javascript
console.log['cafe\u0301']; // => 'café'
console.log['café']; // => 'café'
Thỏa thuận giữa hai máy tính về sự tương ứng giữa các chữ cái và số là những gì Unicode chuẩn hóa
Về mặt Unicode,
3 là một ký tự trừu tượng có tên LATIN SMALL CHỮ H. Ký tự này có số tương ứngjavascript
console.log['cafe\u0301']; // => 'café'
console.log['café']; // => 'café'
4, là một điểm mã trong ký hiệujavascript
console.log['cafe\u0301']; // => 'café'
console.log['café']; // => 'café'
1javascript
const smile = '\uD83D\uDE00';
console.log[smile]; // => '😀'
console.log[smile.length]; // => 2
const letter = 'e\u0301';
console.log[letter]; // => 'é'
console.log[letter.length]; // => 2
Vai trò của Unicode là cung cấp một danh sách các ký tự trừu tượng [bộ ký tự] và gán cho mỗi ký tự một điểm mã định danh duy nhất [bộ ký tự được mã hóa]
2. Thuật ngữ Unicode cơ bản
Các trang web
2 đề cập đếnjavascript
const smile = '\uD83D\uDE00';
console.log[smile]; // => '😀'
console.log[smile.length]; // => 2
const letter = 'e\u0301';
console.log[letter]; // => 'é'
console.log[letter.length]; // => 2
Unicode cung cấp một số duy nhất cho mọi ký tự, bất kể nền tảng nào, bất kể chương trình nào, bất kể ngôn ngữ nào
Unicode là một bộ ký tự phổ quát xác định danh sách các ký tự từ phần lớn các hệ thống chữ viết và liên kết cho mỗi ký tự một số duy nhất [điểm mã]
Unicode bao gồm các ký tự từ hầu hết các ngôn ngữ ngày nay, dấu chấm câu, dấu phụ, ký hiệu toán học, ký hiệu kỹ thuật, mũi tên, biểu tượng cảm xúc, v.v.
Phiên bản Unicode đầu tiên 1. 0 được xuất bản vào tháng 10 năm 1991 và có 7.161 ký tự. Phiên bản mới nhất 14. 0 [xuất bản vào tháng 9 năm 2021] cung cấp mã cho 144.697 ký tự
Cách tiếp cận toàn diện và toàn diện của Unicode giải quyết được một vấn đề lớn tồn tại trước đây khi các nhà cung cấp triển khai rất nhiều bộ ký tự và bảng mã khó xử lý
Thật phức tạp để tạo một ứng dụng hỗ trợ tất cả các bộ ký tự và mã hóa
Nếu bạn nghĩ Unicode đã khó thì lập trình không có Unicode còn khó hơn
Tôi vẫn nhớ mình đã chọn các bộ ký tự và bảng mã ngẫu nhiên để đọc nội dung của các tệp. xổ số thuần túy
2. 1 Ký tự và điểm mã
Ký tự trừu tượng [hoặc ký tự] là một đơn vị thông tin được sử dụng để tổ chức, kiểm soát hoặc biểu diễn dữ liệu văn bản
Unicode xử lý các ký tự dưới dạng thuật ngữ trừu tượng. Mỗi ký tự trừu tượng có một tên liên kết, e. g. CHỮ NHỎ LATIN A. Hình thức kết xuất [glyph] của ký tự này là
3javascript
const smile = '\uD83D\uDE00';
console.log[smile]; // => '😀'
console.log[smile.length]; // => 2
const letter = 'e\u0301';
console.log[letter]; // => 'é'
console.log[letter.length]; // => 2
Điểm mã là một số được gán cho một ký tự
Điểm mã là các số trong phạm vi từ
4 đếnjavascript
const smile = '\uD83D\uDE00';
console.log[smile]; // => '😀'
console.log[smile.length]; // => 2
const letter = 'e\u0301';
console.log[letter]; // => 'é'
console.log[letter.length]; // => 2
5javascript
const smile = '\uD83D\uDE00';
console.log[smile]; // => '😀'
console.log[smile.length]; // => 2
const letter = 'e\u0301';
console.log[letter]; // => 'é'
console.log[letter.length]; // => 2
6 là định dạng của các điểm mã, trong đójavascript
const smile = '\uD83D\uDE00';
console.log[smile]; // => '😀'
console.log[smile.length]; // => 2
const letter = 'e\u0301';
console.log[letter]; // => 'é'
console.log[letter.length]; // => 2
7 là tiền tố có nghĩa là Unicode vàjavascript
const smile = '\uD83D\uDE00';
console.log[smile]; // => '😀'
console.log[smile.length]; // => 2
const letter = 'e\u0301';
console.log[letter]; // => 'é'
console.log[letter.length]; // => 2
8 là một số ở hệ thập lục phân. Ví dụ:javascript
const smile = '\uD83D\uDE00';
console.log[smile]; // => '😀'
console.log[smile.length]; // => 2
const letter = 'e\u0301';
console.log[letter]; // => 'é'
console.log[letter.length]; // => 2
9 vàjavascript
const smile = '\uD83D\uDE00';
console.log[smile]; // => '😀'
console.log[smile.length]; // => 2
const letter = 'e\u0301';
console.log[letter]; // => 'é'
console.log[letter.length]; // => 2
0 là các điểm mãjavascript
const str = '\x4A\x61vaScript';
console.log[str]; // => 'JavaScript'
const reg = /\x4A\x61va.*/;
console.log[reg.test['JavaScript']]; // => true
Hãy nhớ rằng một điểm mã là một số đơn giản. Và đó là cách bạn nên nghĩ về nó. Điểm mã là một loại chỉ số của một phần tử trong một mảng
Điều kỳ diệu xảy ra vì Unicode liên kết một điểm mã với một ký tự. Ví dụ:
9 tương ứng với ký tự có tên CHỮ VỐN LATIN A [được hiển thị làjavascript
const smile = '\uD83D\uDE00';
console.log[smile]; // => '😀'
console.log[smile.length]; // => 2
const letter = 'e\u0301';
console.log[letter]; // => 'é'
console.log[letter.length]; // => 2
2] hoặcjavascript
const str = '\x4A\x61vaScript';
console.log[str]; // => 'JavaScript'
const reg = /\x4A\x61va.*/;
console.log[reg.test['JavaScript']]; // => true
0 tương ứng với ký tự có tên SNOWMAN [được hiển thị làjavascript
const str = '\x4A\x61vaScript';
console.log[str]; // => 'JavaScript'
const reg = /\x4A\x61va.*/;
console.log[reg.test['JavaScript']]; // => true
4]javascript
const str = '\x4A\x61vaScript';
console.log[str]; // => 'JavaScript'
const reg = /\x4A\x61va.*/;
console.log[reg.test['JavaScript']]; // => true
Không phải tất cả các điểm mã đều có các ký tự liên quan. Có sẵn
5 điểm mã [phạm vijavascript
const str = '\x4A\x61vaScript';
console.log[str]; // => 'JavaScript'
const reg = /\x4A\x61va.*/;
console.log[reg.test['JavaScript']]; // => true
4 đếnjavascript
const smile = '\uD83D\uDE00';
console.log[smile]; // => '😀'
console.log[smile.length]; // => 2
const letter = 'e\u0301';
console.log[letter]; // => 'é'
console.log[letter.length]; // => 2
5], nhưng chỉjavascript
const smile = '\uD83D\uDE00';
console.log[smile]; // => '😀'
console.log[smile.length]; // => 2
const letter = 'e\u0301';
console.log[letter]; // => 'é'
console.log[letter.length]; // => 2
8 [kể từ tháng 9 năm 2021] có các ký tự liên quanjavascript
const str = '\x4A\x61vaScript';
console.log[str]; // => 'JavaScript'
const reg = /\x4A\x61va.*/;
console.log[reg.test['JavaScript']]; // => true
2. 2 mặt phẳng Unicode
Mặt phẳng là một phạm vi gồm 65.536 [hoặc 1000016] điểm mã Unicode liền kề từ
9 đếnjavascript
const str = '\x4A\x61vaScript';
console.log[str]; // => 'JavaScript'
const reg = /\x4A\x61va.*/;
console.log[reg.test['JavaScript']]; // => true
0, trong đójavascript
const str = 'I\u0020learn \u0055nicode';
console.log[str]; // => 'I learn Unicode'
const reg = /\u0055ni.*/;
console.log[reg.test['Unicode']]; // => true
1 có thể nhận các giá trị từ 016 đến 1016javascript
const str = 'I\u0020learn \u0055nicode';
console.log[str]; // => 'I learn Unicode'
const reg = /\u0055ni.*/;
console.log[reg.test['Unicode']]; // => true
Toàn bộ các điểm mã Unicode được chia thành 17 mặt phẳng
- Mặt phẳng 0 chứa các điểm mã từ
4 đếnjavascript
const smile = '\uD83D\uDE00';
console.log[smile]; // => '😀'
console.log[smile.length]; // => 2
const letter = 'e\u0301';
console.log[letter]; // => 'é'
console.log[letter.length]; // => 2
3,javascript
const str = 'I\u0020learn \u0055nicode';
console.log[str]; // => 'I learn Unicode'
const reg = /\u0055ni.*/;
console.log[reg.test['Unicode']]; // => true
- Mặt phẳng 1 chứa các điểm mã từ
4 đếnjavascript
const str = 'I\u0020learn \u0055nicode';
console.log[str]; // => 'I learn Unicode'
const reg = /\u0055ni.*/;
console.log[reg.test['Unicode']]; // => true
5javascript
const str = 'I\u0020learn \u0055nicode';
console.log[str]; // => 'I learn Unicode'
const reg = /\u0055ni.*/;
console.log[reg.test['Unicode']]; // => true
- ...
- Mặt phẳng 16 chứa các điểm mã từ
6 đếnjavascript
const str = 'I\u0020learn \u0055nicode';
console.log[str]; // => 'I learn Unicode'
const reg = /\u0055ni.*/;
console.log[reg.test['Unicode']]; // => true
7javascript
const str = 'I\u0020learn \u0055nicode';
console.log[str]; // => 'I learn Unicode'
const reg = /\u0055ni.*/;
console.log[reg.test['Unicode']]; // => true
Máy bay đa ngôn ngữ cơ bản
Mặt phẳng 0 là một mặt phẳng đặc biệt, có tên là Mặt phẳng đa ngôn ngữ cơ bản hoặc ngắn gọn là BMP. Nó chứa các ký tự từ hầu hết các ngôn ngữ hiện đại [Tiếng Latinh cơ bản, Cyrillic, Hy Lạp, v.v.] và một số lượng lớn các ký hiệu
Như đã đề cập ở trên, các điểm mã từ Mặt phẳng đa ngôn ngữ cơ bản nằm trong phạm vi từ
4 đếnjavascript
const smile = '\uD83D\uDE00';
console.log[smile]; // => '😀'
console.log[smile.length]; // => 2
const letter = 'e\u0301';
console.log[letter]; // => 'é'
console.log[letter.length]; // => 2
3 và có thể có tối đa 4 chữ số thập lục phânjavascript
const str = 'I\u0020learn \u0055nicode';
console.log[str]; // => 'I learn Unicode'
const reg = /\u0055ni.*/;
console.log[reg.test['Unicode']]; // => true
Nhà phát triển thường xử lý các ký tự từ BMP
Một số nhân vật từ BMP
5 làjavascript
console.log['cafe\u0301']; // => 'café'
console.log['café']; // => 'café'
1 có tên là CHỮ NHỎ LATIN Ejavascript
const str = 'My face \uD83D\uDE00';
console.log[str]; // => 'My face 😀'
2 làjavascript
const str = 'My face \uD83D\uDE00';
console.log[str]; // => 'My face 😀'
3 tên là THANH ĐỨNGjavascript
const str = 'My face \uD83D\uDE00';
console.log[str]; // => 'My face 😀'
4 làjavascript
const str = 'My face \uD83D\uDE00';
console.log[str]; // => 'My face 😀'
5 có tên VUÔNG ĐENjavascript
const str = 'My face \uD83D\uDE00';
console.log[str]; // => 'My face 😀'
6 làjavascript
const str = 'My face \uD83D\uDE00';
console.log[str]; // => 'My face 😀'
7 tên là UMBRELLAjavascript
const str = 'My face \uD83D\uDE00';
console.log[str]; // => 'My face 😀'
cõi trung giới
16 mặt phẳng ngoài BMP [Mặt phẳng 1, Mặt phẳng 2,. , Mặt phẳng 16] được đặt tên là mặt phẳng trung giới hoặc mặt phẳng bổ sung
Các điểm mã là một phần của các mặt phẳng trung gian được đặt tên là các điểm mã trung gian. Các điểm mã này nằm trong phạm vi từ
8 đếnjavascript
const str = 'My face \uD83D\uDE00';
console.log[str]; // => 'My face 😀'
5javascript
const smile = '\uD83D\uDE00';
console.log[smile]; // => '😀'
console.log[smile.length]; // => 2
const letter = 'e\u0301';
console.log[letter]; // => 'é'
console.log[letter.length]; // => 2
Một điểm mã thiên văn có thể có 5 hoặc 6 chữ số ở dạng thập lục phân.
0 hoặcjavascript
const str = 'Funny cat \u{1F639}';
console.log[str]; // => 'Funny cat 😹'
const reg = /\u{1F639}/u;
console.log[reg.test['Funny cat 😹']]; // => true
1javascript
const str = 'Funny cat \u{1F639}';
console.log[str]; // => 'Funny cat 😹'
const reg = /\u{1F639}/u;
console.log[reg.test['Funny cat 😹']]; // => true
Chúng ta hãy xem một số nhân vật từ máy bay astral
2 làjavascript
const str = 'Funny cat \u{1F639}';
console.log[str]; // => 'Funny cat 😹'
const reg = /\u{1F639}/u;
console.log[reg.test['Funny cat 😹']]; // => true
3 được đặt tên là BIỂU TƯỢNG ÂM NHẠC G CLEFjavascript
const str = 'Funny cat \u{1F639}';
console.log[str]; // => 'Funny cat 😹'
const reg = /\u{1F639}/u;
console.log[reg.test['Funny cat 😹']]; // => true
4 làjavascript
const str = 'Funny cat \u{1F639}';
console.log[str]; // => 'Funny cat 😹'
const reg = /\u{1F639}/u;
console.log[reg.test['Funny cat 😹']]; // => true
5 tên là TOÁN VỐN ĐẬM Bjavascript
const str = 'Funny cat \u{1F639}';
console.log[str]; // => 'Funny cat 😹'
const reg = /\u{1F639}/u;
console.log[reg.test['Funny cat 😹']]; // => true
6 làjavascript
const str = 'Funny cat \u{1F639}';
console.log[str]; // => 'Funny cat 😹'
const reg = /\u{1F639}/u;
console.log[reg.test['Funny cat 😹']]; // => true
7 tên là DOMINO TITLE NGANG-00-04javascript
const str = 'Funny cat \u{1F639}';
console.log[str]; // => 'Funny cat 😹'
const reg = /\u{1F639}/u;
console.log[reg.test['Funny cat 😹']]; // => true
8 làjavascript
const str = 'Funny cat \u{1F639}';
console.log[str]; // => 'Funny cat 😹'
const reg = /\u{1F639}/u;
console.log[reg.test['Funny cat 😹']]; // => true
9 được đặt tên là MẶT NỤ CƯỜIjavascript
const str = 'Funny cat \u{1F639}';
console.log[str]; // => 'Funny cat 😹'
const reg = /\u{1F639}/u;
console.log[reg.test['Funny cat 😹']]; // => true
2. 3 đơn vị mã
Ok, các ký tự Unicode, điểm mã và mặt phẳng là trừu tượng
Nhưng bây giờ hãy xem cách Unicode được triển khai ở cấp độ vật lý, phần cứng
Máy tính ở cấp độ bộ nhớ không sử dụng điểm mã hoặc ký tự trừu tượng. Nó cần một cách vật lý để thể hiện các điểm mã Unicode. đơn vị mã
Đơn vị mã là dãy bit dùng để mã hóa từng ký tự trong một dạng mã hóa nhất định
Mã hóa ký tự là thứ biến đổi các điểm mã trừu tượng thành các bit vật lý. đơn vị mã. Nói cách khác, mã hóa ký tự dịch các điểm mã Unicode thành các chuỗi đơn vị mã duy nhất
Các bảng mã phổ biến là UTF-8, UTF-16 và UTF-32
Hầu hết các công cụ JavaScript sử dụng mã hóa UTF-16, vì vậy hãy xem chi tiết về UTF-16
UTF-16 [tên dài. 16-bit Unicode Transformation Format] là một mã hóa có độ dài thay đổi
- Các điểm mã từ BMP được mã hóa bằng một đơn vị mã 16-bit
- Các điểm mã từ cõi trung giới được mã hóa bằng hai đơn vị mã, mỗi đơn vị 16 bit
Ok, đó sẽ là tất cả cho lý thuyết khô khan. Hãy làm theo một số ví dụ
Giả sử bạn muốn lưu vào ổ cứng LATIN CHỮ NHỎ MỘT ký tự
3. Unicode cho bạn biết rằng CHỮ NHỎ LATIN Một ký tự trừu tượng ánh xạ tới điểm mãjavascript
const smile = '\uD83D\uDE00';
console.log[smile]; // => '😀'
console.log[smile.length]; // => 2
const letter = 'e\u0301';
console.log[letter]; // => 'é'
console.log[letter.length]; // => 2
1javascript
const niceEmoticon = '\u{1F607}';
console.log[niceEmoticon]; // => '😇'
const spNiceEmoticon = '\uD83D\uDE07'
console.log[spNiceEmoticon]; // => '😇'
console.log[niceEmoticon === spNiceEmoticon]; // => true
Bây giờ, hãy hỏi mã hóa UTF-16 nên chuyển đổi
1 như thế nào. Thông số kỹ thuật mã hóa nói rằng đối với điểm mã BMP, hãy lấy số thập lục phân của nó làjavascript
const niceEmoticon = '\u{1F607}';
console.log[niceEmoticon]; // => '😇'
const spNiceEmoticon = '\uD83D\uDE07'
console.log[spNiceEmoticon]; // => '😇'
console.log[niceEmoticon === spNiceEmoticon]; // => true
3 và lưu trữ nó vào một đơn vị mã 16-bit.javascript
const niceEmoticon = '\u{1F607}';
console.log[niceEmoticon]; // => '😇'
const spNiceEmoticon = '\uD83D\uDE07'
console.log[spNiceEmoticon]; // => '😇'
console.log[niceEmoticon === spNiceEmoticon]; // => true
4javascript
const niceEmoticon = '\u{1F607}';
console.log[niceEmoticon]; // => '😇'
const spNiceEmoticon = '\uD83D\uDE07'
console.log[spNiceEmoticon]; // => '😇'
console.log[niceEmoticon === spNiceEmoticon]; // => true
Như bạn có thể thấy, các điểm mã từ BMP phù hợp với một đơn vị mã 16-bit duy nhất
2. 4 cặp thay thế
Bây giờ chúng ta hãy nghiên cứu một trường hợp phức tạp. Giả sử bạn muốn mã hóa ký tự GRINNING FACE
8. Ký tự này ánh xạ tớijavascript
const str = 'Funny cat \u{1F639}';
console.log[str]; // => 'Funny cat 😹'
const reg = /\u{1F639}/u;
console.log[reg.test['Funny cat 😹']]; // => true
9 điểm mã, từ một cõi trung giớijavascript
const str = 'Funny cat \u{1F639}';
console.log[str]; // => 'Funny cat 😹'
const reg = /\u{1F639}/u;
console.log[reg.test['Funny cat 😹']]; // => true
Vì các điểm mã vũ trụ yêu cầu 21 bit để lưu thông tin, UTF-16 nói rằng bạn cần hai đơn vị mã 16 bit. Điểm mã
9 được chia thành cái gọi là cặp thay thế.javascript
const str = 'Funny cat \u{1F639}';
console.log[str]; // => 'Funny cat 😹'
const reg = /\u{1F639}/u;
console.log[reg.test['Funny cat 😹']]; // => true
8 [đơn vị mã thay thế cao] vàjavascript
const niceEmoticon = '\u{1F607}';
console.log[niceEmoticon]; // => '😇'
const spNiceEmoticon = '\uD83D\uDE07'
console.log[spNiceEmoticon]; // => '😇'
console.log[niceEmoticon === spNiceEmoticon]; // => true
9 [đơn vị mã thay thế thấp]javascript
const niceEmoticon = '\u{1F607}';
console.log[niceEmoticon]; // => '😇'
const spNiceEmoticon = '\uD83D\uDE07'
console.log[spNiceEmoticon]; // => '😇'
console.log[niceEmoticon === spNiceEmoticon]; // => true
Cặp thay thế là biểu diễn cho một ký tự trừu tượng duy nhất bao gồm một chuỗi đơn vị mã gồm hai đơn vị mã 16 bit, trong đó giá trị đầu tiên của cặp là đơn vị mã thay thế cao và giá trị thứ hai là mã thay thế thấp
Một điểm mã thiên thể yêu cầu hai đơn vị mã - một cặp thay thế. e. g. để mã hóa
9 [javascript
const str = 'Funny cat \u{1F639}';
console.log[str]; // => 'Funny cat 😹'
const reg = /\u{1F639}/u;
console.log[reg.test['Funny cat 😹']]; // => true
8] trong UTF-16, một cặp thay thế được sử dụng.javascript
const str = 'Funny cat \u{1F639}';
console.log[str]; // => 'Funny cat 😹'
const reg = /\u{1F639}/u;
console.log[reg.test['Funny cat 😹']]; // => true
02javascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
javascript
console.log['\uD83D\uDE00']; // => '😀'
Đơn vị mã thay thế cao nhận các giá trị từ phạm vi
03 đếnjavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
04. Đơn vị mã thay thế thấp nhận các giá trị từ phạm vijavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
05 đếnjavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
06javascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
Thuật toán chuyển đổi một cặp thay thế thành điểm mã astral và ngược lại như sau
javascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
Các cặp thay thế không thoải mái. Bạn phải xử lý chúng như những trường hợp đặc biệt khi xử lý các chuỗi trong JavaScript, như được mô tả bên dưới trong bài viết
Tuy nhiên, UTF-16 là bộ nhớ hiệu quả. 99% ký tự được xử lý là từ BMP, yêu cầu một đơn vị mã và tiết kiệm rất nhiều bộ nhớ
2. 5 Kết hợp dấu
Một grapheme, hoặc biểu tượng, là một đơn vị viết khác biệt tối thiểu trong ngữ cảnh của một hệ thống chữ viết cụ thể
Biểu đồ là cách người dùng nghĩ về một ký tự. Hình ảnh cụ thể của biểu đồ hiển thị trên màn hình được đặt tên là glyph
Trong nhiều trường hợp, một ký tự Unicode duy nhất đại diện cho một biểu đồ. Ví dụ:
07 CHỮ NHỎ LATIN F là một cách viết tiếng Anhjavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
08javascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
Có những trường hợp khi một biểu đồ chứa một chuỗi các ký tự
Ví dụ,
09 là một grapheme nguyên tử trong hệ thống chữ viết của Đan Mạch. Nó được hiển thị bằng cách sử dụngjavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
1 CHỮ NHỎ LATIN A [được hiển thị làjavascript
const niceEmoticon = '\u{1F607}';
console.log[niceEmoticon]; // => '😇'
const spNiceEmoticon = '\uD83D\uDE07'
console.log[spNiceEmoticon]; // => '😇'
console.log[niceEmoticon === spNiceEmoticon]; // => true
3] kết hợp với một ký tự đặc biệtjavascript
const smile = '\uD83D\uDE00';
console.log[smile]; // => '😀'
console.log[smile.length]; // => 2
const letter = 'e\u0301';
console.log[letter]; // => 'é'
console.log[letter.length]; // => 2
12 KẾT HỢP VÒNG TRÊN [được hiển thị là ◌̊]javascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
12 sửa đổi ký tự tiền lệ và được đặt tên là dấu kết hợpjavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
javascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
Dấu kết hợp là một ký tự áp dụng cho ký tự cơ sở tiền lệ để tạo biểu đồ mới
Các dấu kết hợp bao gồm các ký tự như dấu trọng âm, dấu phụ, điểm tiếng Do Thái, dấu nguyên âm tiếng Ả Rập và dấu chỉ thị
Các dấu kết hợp thường không được sử dụng riêng lẻ, tôi. e. không có ký tự cơ sở. Bạn nên tránh hiển thị chúng bị cô lập
Cũng như các cặp thay thế, các dấu kết hợp rất khó xử lý trong JavaScript
Một chuỗi ký tự kết hợp [ký tự cơ sở + dấu kết hợp] được người dùng phân biệt dưới dạng một ký hiệu đơn [e. g.
14 làjavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
15]. Nhưng nhà phát triển phải xác định rằng 2 điểm mãjavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
1 vàjavascript
const niceEmoticon = '\u{1F607}';
console.log[niceEmoticon]; // => '😇'
const spNiceEmoticon = '\uD83D\uDE07'
console.log[spNiceEmoticon]; // => '😇'
console.log[niceEmoticon === spNiceEmoticon]; // => true
12 được sử dụng để xây dựngjavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
09javascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
3. Unicode trong JavaScript
Đặc tả ES2015 mà văn bản mã nguồn được thể hiện bằng Unicode [phiên bản 5. 1 trở lên]. Văn bản nguồn là một chuỗi các điểm mã từ
4 đếnjavascript
const smile = '\uD83D\uDE00';
console.log[smile]; // => '😀'
console.log[smile.length]; // => 2
const letter = 'e\u0301';
console.log[letter]; // => 'é'
console.log[letter.length]; // => 2
5. Cách mã nguồn được lưu trữ hoặc trao đổi không liên quan đến đặc tả ECMAScript, nhưng thường được mã hóa bằng UTF-8 [mã hóa ưa thích cho web]javascript
const smile = '\uD83D\uDE00';
console.log[smile]; // => '😀'
console.log[smile.length]; // => 2
const letter = 'e\u0301';
console.log[letter]; // => 'é'
console.log[letter.length]; // => 2
Tôi khuyên bạn nên giữ nguyên văn bản mã nguồn với các ký tự từ khối Unicode Latinh Cơ bản [hoặc ASCII]. Các ký tự bên ngoài ASCII phải được thoát. Điều này sẽ đảm bảo ít vấn đề hơn khi mã hóa
Bên trong, ở cấp độ ngôn ngữ, ECMAScript 2015 cung cấp rõ ràng chuỗi nào có trong JavaScript
Loại Chuỗi là tập hợp tất cả các chuỗi được sắp xếp bằng 0 hoặc nhiều giá trị số nguyên không dấu 16 bit ["phần tử"] lên đến độ dài tối đa là 253-1 phần tử. Loại Chuỗi thường được sử dụng để biểu thị dữ liệu văn bản trong chương trình ECMAScript đang chạy, trong trường hợp đó, mỗi phần tử trong Chuỗi được coi là giá trị đơn vị mã UTF-16
Mọi phần tử của chuỗi được động cơ diễn giải dưới dạng đơn vị mã. Cách một chuỗi được hiển thị không cung cấp một cách xác định để quyết định đơn vị mã nào [đại diện cho các điểm mã] mà nó chứa. Xem ví dụ sau
javascript
console.log['cafe\u0301']; // => 'café'
console.log['café']; // => 'café'
21 vàjavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
22 chữ có đơn vị mã hơi khác nhau, nhưng cả hai đều được hiển thị cùng một chuỗi ký hiệujavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
23javascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
Độ dài của một String là số phần tử [i. e. , giá trị 16 bit] bên trong nó. [. ] Khi các hoạt động ECMAScript diễn giải các giá trị Chuỗi, mỗi phần tử được diễn giải dưới dạng một đơn vị mã UTF-16
Như bạn đã biết từ các cặp thay thế và dấu kết hợp ở các chương trên, một số ký hiệu cần 2 đơn vị mã trở lên để được biểu diễn. Vì vậy, bạn nên đề phòng khi đếm số lượng ký tự hoặc truy cập ký tự theo chỉ mục
javascript
const smile = '\uD83D\uDE00';
console.log[smile]; // => '😀'
console.log[smile.length]; // => 2
const letter = 'e\u0301';
console.log[letter]; // => 'é'
console.log[letter.length]; // => 2
24 chuỗi chứa 2 đơn vị mã.javascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
25 [đại diện cao] vàjavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
26 [đại diện thấp]. Vì chuỗi là một chuỗi các đơn vị mã, nênjavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
27 ước tính thànhjavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
28. Ngay cả khi kết xuấtjavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
24 chỉ có một biểu tượngjavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
30javascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
Kịch bản tương tự cũng xảy ra với chuỗi
31. Dấu kết hợpjavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
32 áp dụng cho ký tự trước đó và kết quả hiển thị là một ký hiệujavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
33. Tuy nhiên,javascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
31 chứajavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
28 đơn vị mã, do đó,javascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
36 làjavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
28javascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
Lời khuyên của tôi. luôn coi chuỗi trong JavaScript là một chuỗi các đơn vị mã. Cách hiển thị chuỗi trên màn hình không thể nói rõ nó chứa những đơn vị mã nào
Các ký hiệu thiên thể và các chuỗi ký tự kết hợp yêu cầu 2 đơn vị mã trở lên được mã hóa nhưng được coi là một biểu đồ đơn. Nếu một chuỗi có các cặp thay thế hoặc các dấu kết hợp, bạn có thể bối rối khi đánh giá độ dài chuỗi hoặc truy cập một ký tự theo chỉ mục mà không ghi nhớ ý tưởng này
Hầu hết các phương thức chuỗi JavaScript không nhận biết Unicode. Nếu chuỗi của bạn chứa các ký tự Unicode ghép, hãy thận trọng khi sử dụng
38,javascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
39, v.v.javascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
3. 1 trình tự thoát
Trình tự thoát trong chuỗi được sử dụng để thể hiện các đơn vị mã dựa trên số điểm mã. JavaScript có 3 loại thoát, một loại đã được giới thiệu trong ECMAScript 2015
Hãy xem chúng chi tiết hơn
Trình tự thoát thập lục phân
Hình thức ngắn nhất được đặt tên là chuỗi thoát thập lục phân.
40, trong đójavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
41 là tiền tố theo sau bởi số thập lục phânjavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
8 với độ dài cố định là 2 chữ sốjavascript
const smile = '\uD83D\uDE00';
console.log[smile]; // => '😀'
console.log[smile.length]; // => 2
const letter = 'e\u0301';
console.log[letter]; // => 'é'
console.log[letter.length]; // => 2
Ví dụ:
43 [ký hiệujavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
44] hoặcjavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
45 [ký hiệujavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
46]javascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
Trình tự thoát thập lục phân trong một chuỗi ký tự hoặc biểu thức chính quy trông như thế này
javascript
const str = '\x4A\x61vaScript';
console.log[str]; // => 'JavaScript'
const reg = /\x4A\x61va.*/;
console.log[reg.test['JavaScript']]; // => true
Chuỗi thoát thập lục phân có thể thoát khỏi các điểm mã trong một phạm vi giới hạn. từ
47 đếnjavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
48 vì chỉ cho phép 2 chữ số. Nhưng thoát thập lục phân là tốt vì nó ngắnjavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
Trình tự thoát Unicode
Nếu bạn muốn thoát các điểm mã khỏi toàn bộ BMP, thì hãy sử dụng chuỗi thoát unicode. Định dạng thoát là
49, trong đójavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
50 là tiền tố theo sau là số thập lục phânjavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
8 với độ dài cố định là 4 chữ số. Ví dụ:javascript
const smile = '\uD83D\uDE00';
console.log[smile]; // => '😀'
console.log[smile.length]; // => 2
const letter = 'e\u0301';
console.log[letter]; // => 'é'
console.log[letter.length]; // => 2
52 [ký hiệujavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
53] hoặcjavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
54 [ký hiệu tích phânjavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
55]javascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
Hãy sử dụng một số trình tự thoát unicode
javascript
const str = 'I\u0020learn \u0055nicode';
console.log[str]; // => 'I learn Unicode'
const reg = /\u0055ni.*/;
console.log[reg.test['Unicode']]; // => true
Trình tự thoát unicode có thể thoát khỏi các điểm mã trong một phạm vi giới hạn. từ
4 đếnjavascript
const smile = '\uD83D\uDE00';
console.log[smile]; // => '😀'
console.log[smile.length]; // => 2
const letter = 'e\u0301';
console.log[letter]; // => 'é'
console.log[letter.length]; // => 2
3 [tất cả các điểm mã BMP] vì chỉ có 4 chữ số được phép. Chủ yếu là đủ để đại diện cho các ký hiệu thường được sử dụngjavascript
const str = 'I\u0020learn \u0055nicode';
console.log[str]; // => 'I learn Unicode'
const reg = /\u0055ni.*/;
console.log[reg.test['Unicode']]; // => true
Để biểu thị một biểu tượng thiên văn bằng chữ JavaScript, bạn phải sử dụng hai chuỗi thoát unicode đã nối [một đại diện thay thế cao và đại diện thấp], tạo ra một cặp thay thế
javascript
const str = 'My face \uD83D\uDE00';
console.log[str]; // => 'My face 😀'
58 là một cặp thay thế được tạo bằng 2 chuỗi thoátjavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
Trình tự thoát điểm mã
ECMAScript 2015 cung cấp các chuỗi thoát đại diện cho các điểm mã từ toàn bộ không gian Unicode.
4 đếnjavascript
const smile = '\uD83D\uDE00';
console.log[smile]; // => '😀'
console.log[smile.length]; // => 2
const letter = 'e\u0301';
console.log[letter]; // => 'é'
console.log[letter.length]; // => 2
5, tôi. e. BMP và máy bay vũ trụjavascript
const smile = '\uD83D\uDE00';
console.log[smile]; // => '😀'
console.log[smile.length]; // => 2
const letter = 'e\u0301';
console.log[letter]; // => 'é'
console.log[letter.length]; // => 2
Định dạng mới được gọi là trình tự thoát điểm mã.
61, trong đójavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
8 là số thập lục phân có độ dài thay đổi từ 1 đến 6 chữ sốjavascript
const smile = '\uD83D\uDE00';
console.log[smile]; // => '😀'
console.log[smile.length]; // => 2
const letter = 'e\u0301';
console.log[letter]; // => 'é'
console.log[letter.length]; // => 2
Ví dụ:
63 [ký hiệujavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
64] hoặcjavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
65 [ký hiệu chú mèo ngộ nghĩnhjavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
66]javascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
Hãy xem cách bạn có thể sử dụng nó theo nghĩa đen
javascript
const str = 'Funny cat \u{1F639}';
console.log[str]; // => 'Funny cat 😹'
const reg = /\u{1F639}/u;
console.log[reg.test['Funny cat 😹']]; // => true
Biểu thức chính quy
67 có một cờ đặc biệtjavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
68, cho phép các tính năng Unicode bổ sung [xem thêm về điều đó trong]javascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
Tôi thích cách thoát điểm mã đó loại bỏ cặp thay thế để biểu thị biểu tượng thiên văn. Ví dụ: hãy thoát khỏi
69 MẶT CƯỜI VỚI điểm mã HALOjavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
javascript
const niceEmoticon = '\u{1F607}';
console.log[niceEmoticon]; // => '😇'
const spNiceEmoticon = '\uD83D\uDE07'
console.log[spNiceEmoticon]; // => '😇'
console.log[niceEmoticon === spNiceEmoticon]; // => true
Chuỗi ký tự được gán cho biến
70 có một điểm thoát mãjavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
71 đại diện cho một điểm mã astraljavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
69javascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
Tuy nhiên, dưới vỏ bọc, thoát điểm mã tạo ra một cặp thay thế [2 đơn vị mã].
73 [được tạo bằng cách sử dụng cặp thoát unicode thay thếjavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
74] bằng vớijavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
70javascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
Khi một biểu thức chính quy được tạo bằng cách sử dụng hàm tạo
76, trong chuỗi ký tự, bạn phải thay thế từngjavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
77 bằngjavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
78 để biểu thị thoát unicode. Các đối tượng biểu thức chính quy sau đây là tương đươngjavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
0javascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
3. So sánh 2 chuỗi
Các chuỗi trong JavaScript là các chuỗi đơn vị mã. Bạn có thể mong đợi rằng việc so sánh chuỗi liên quan đến việc đánh giá các đơn vị mã cho phù hợp. nếu các đơn vị mã từ cả hai chuỗi bằng nhau
Cách này nhanh và hiệu quả. Nó hoạt động độc đáo với các chuỗi "đơn giản"
1javascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
Các chuỗi
79 vàjavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
80 có cùng một chuỗi đơn vị mã — chúng bằng nhaujavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
Tuy nhiên, giả sử bạn muốn so sánh hai chuỗi trông giống nhau được hiển thị nhưng chứa các chuỗi đơn vị mã khác nhau. Sau đó, bạn có thể có một kết quả không mong muốn, bởi vì các chuỗi trông giống nhau về mặt trực quan lại không bằng nhau khi so sánh
2javascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
81 vàjavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
82 khi hiển thị trông giống nhau, nhưng có các đơn vị mã khác nhaujavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
Nó xảy ra bởi vì ________ 72 ______ 83 graphene có thể được cấu tạo theo hai cách
- Sử dụng
84 CHỮ C NHỎ LATIN VỚI CEDILLAjavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
- Hoặc sử dụng một chuỗi ký tự kết hợp.
85 CHỮ C CHỮ NHỎ LATIN cộng với dấu kết hợpjavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
86 CEDILLA KẾT HỢPjavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
Làm thế nào để xử lý các tình huống như vậy và so sánh chính xác các chuỗi?
bình thường hóa
Chuẩn hóa là một chuyển đổi chuỗi thành một biểu diễn chính tắc, để đảm bảo rằng các chuỗi tương đương chính tắc [và/hoặc tương đương tương thích] có các biểu diễn duy nhất
Nói cách khác, khi một chuỗi có cấu trúc phức tạp chứa các chuỗi ký tự kết hợp hoặc các cấu trúc phức hợp khác, bạn có thể chuẩn hóa nó thành dạng chuẩn. Các chuỗi được chuẩn hóa không gây khó khăn khi so sánh hoặc thực hiện các thao tác chuỗi như tìm kiếm văn bản, v.v.
Unicode Standard Annex #15 có các chi tiết thú vị về quá trình chuẩn hóa
Trong JavaScript để chuẩn hóa một chuỗi gọi phương thức
87, có sẵn trong ES2015.javascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
88 là một tham số tùy chọn [mặc định làjavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
89] và có thể có một trong các dạng chuẩn hóa saujavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
89 dưới dạng Chuẩn hóa Hình thức Thành phần Chính tắcjavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
91 dưới dạng Chuẩn hóa Phân rã Canonicaljavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
92 dưới dạng Thành phần Tương thích Biểu mẫu Chuẩn hóajavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
93 dưới dạng Phân rã tương thích biểu mẫu chuẩn hóajavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
Hãy cải thiện ví dụ trước bằng cách áp dụng chuẩn hóa chuỗi để so sánh chính xác các chuỗi
3javascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
Khi
94 được gọi, một phiên bản chính tắc củajavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
82 được trả về [_______72_______96 được thay thế bằngjavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
97]. Vì vậy, phép so sánhjavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
98 trả vềjavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
99 như mong đợijavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
81 không bị ảnh hưởng bởi quá trình chuẩn hóa, vì nó đã ở dạng chính tắcjavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
3. 3 Chiều dài chuỗi
Tất nhiên, cách phổ biến để xác định độ dài chuỗi là đọc thuộc tính
01. Thuộc tính này cho biết số đơn vị mã mà một chuỗi cójavascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
Việc đánh giá độ dài chuỗi chứa các điểm mã từ BMP thường hoạt động như mong đợi
4javascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
Mỗi đơn vị mã trong
02 tương ứng với một biểu đồ. Độ dài dự kiến của chuỗi làjavascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
03javascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
Chiều dài và các cặp thay thế
Tình huống trở nên khó khăn khi một chuỗi chứa các cặp thay thế, để biểu thị các điểm mã trung gian. Vì mỗi cặp đại diện chứa 2 đơn vị mã [đại diện cao và đại diện thấp], nên thuộc tính độ dài lớn hơn dự kiến
Hãy xem ví dụ
5javascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
Khi chuỗi
04 được hiển thị, nó chứa 4 ký hiệujavascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
05javascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
Tuy nhiên,
27 đánh giá thànhjavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
03, bởi vìjavascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
08 là một điểm mã thiên thể được mã hóa bằng 2 đơn vị mã [một cặp thay thế]javascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
Rất tiếc, hiện tại không có cách tự nhiên và hiệu quả để khắc phục sự cố
Ít nhất ECMAScript 2015 đã giới thiệu các thuật toán nhận biết các ký hiệu thiên thể. Biểu tượng thiên văn được tính là một ký tự, thậm chí được mã hóa bằng 2 đơn vị mã
Nhận biết Unicode là trình lặp chuỗi
09. Bạn có thể kết hợp chuỗi với toán tử trải rộng hàmjavascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
10 hoặc hàmjavascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
11 [cả hai đều sử dụng trình lặp chuỗi]. Sau đó đếm số ký hiệu trong mảng được trả vềjavascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
Tuy nhiên, giải pháp trên có thể khiến bạn giảm nhẹ hiệu suất khi sử dụng rộng rãi
Hãy cải thiện ví dụ trên bằng toán tử trải rộng
6javascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
10 tạo một mảng gồm 4 ký hiệu. Cặp thay thế mã hóajavascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
08 CAT FACE WITH TEARS OF JOY 😹 được giữ nguyên vì trình lặp chuỗi nhận biết Unicodejavascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
Độ dài và dấu kết hợp
Điều gì về việc kết hợp các chuỗi ký tự?
Sự cố được giải quyết khi chuẩn hóa chuỗi. Nếu bạn may mắn, chuỗi ký tự kết hợp được chuẩn hóa thành một ký tự. Hãy thử
7javascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
Chuỗi
14 chứa 5 đơn vị mã [do đó,javascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
15 làjavascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
03], ngay cả khi được hiển thị, nó sẽ hiển thị 4 ký hiệujavascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
Khi chuẩn hóa
14, may mắn là chuỗi ký tự kết hợpjavascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
18 có dạng chính tắcjavascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
33. Vì vậy,javascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
20 chứa các ký hiệujavascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
21 dự kiếnjavascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
Thật không may, chuẩn hóa không phải là một giải pháp phổ quát. Các chuỗi ký tự kết hợp dài không phải lúc nào cũng có các ký tự chính tắc tương đương trong một ký hiệu. Hãy xem một trường hợp như vậy
8javascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
14 có 6 đơn vị mã vàjavascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
15 đánh giá thànhjavascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
24. Tuy nhiênjavascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
14 có 4 ký hiệujavascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
Quá trình chuẩn hóa
26 biến đổi chuỗi kết hợpjavascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
27 thành một dạng chính tắc gồm hai ký tựjavascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
28 [bằng cách chỉ xóa một dấu kết hợp]javascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
Đáng buồn thay,
20 đánh giá thànhjavascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
03 và vẫn không chỉ ra số lượng biểu tượng trực quan mong đợijavascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
3. 4 Định vị ký tự
Vì chuỗi là một chuỗi các đơn vị mã nên việc truy cập ký tự trong chuỗi theo chỉ mục cũng gặp khó khăn
Khi một chuỗi chỉ chứa các ký tự BMP, vị trí ký tự hoạt động chính xác
9javascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
Mỗi biểu tượng được mã hóa bằng một đơn vị mã duy nhất, vì vậy việc truy cập theo chỉ mục một ký tự chuỗi là chính xác
Định vị ký tự và các cặp thay thế
Tình hình thay đổi khi chuỗi chứa các ký hiệu thiên văn
Một biểu tượng thiên văn được mã hóa bằng 2 đơn vị mã [một cặp thay thế]. Vì vậy, việc truy cập các ký tự chuỗi theo chỉ mục có thể trả về một đại diện thay thế cao hoặc đại diện thấp được phân tách, là các ký hiệu không hợp lệ
Ví dụ sau truy cập các ký tự từ một biểu tượng astral
0javascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
Bởi vì
31 MATHEMATICAL BOLD VAPITAL OMEGA là một ký tự astral, nó được mã hóa bằng cách sử dụng một cặp thay thế gồm 2 đơn vị mãjavascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
32 truy cập đơn vị mã thay thế cao vàjavascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
33 thay thế thấp, phá vỡ cặp thay thếjavascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
Có 2 khả năng để truy cập chính xác các biểu tượng thiên thể trong một chuỗi
- Sử dụng trình lặp chuỗi nhận biết Unicode và tạo một mảng ký hiệu
34javascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
- Nhận số điểm mã bằng cách sử dụng
35, sau đó chuyển đổi số thành ký hiệu bằng cách sử dụngjavascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
36 [tùy chọn được đề xuất]javascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
Hãy áp dụng cả hai tùy chọn
1javascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
37 trả về một mảng ký hiệu mà chuỗijavascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
38 chứa. Các cặp thay thế được đánh giá chính xác, vì vậy việc truy cập vào ký tự đầu tiên hoạt động như mong đợi.javascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
39 làjavascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
40javascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
Cuộc gọi phương thức
41 nhận biết Unicode, vì vậy nó trả về số điểm mã astraljavascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
42 của ký tự đầu tiên trong chuỗijavascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
38. Hàmjavascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
36 trả về ký hiệu dựa trên số điểm mã.javascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
40javascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
Định vị ký tự và kết hợp các dấu
Định vị ký tự trong chuỗi có dấu kết hợp có cùng vấn đề với độ dài chuỗi được mô tả ở trên
Truy cập các ký tự theo chỉ mục trong một chuỗi là truy cập các đơn vị mã. Tuy nhiên, chuỗi nhãn kết hợp phải được truy cập tổng thể mà không chia thành các đơn vị mã riêng biệt
Ví dụ sau minh họa vấn đề
2javascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
46 chỉ truy cập ký tự cơ sởjavascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
5, không có dấu kết hợpjavascript
console.log['cafe\u0301']; // => 'café'
console.log['café']; // => 'café'
32 DẤU MẮT KẾT HỢP [được hiển thị là ◌́ ]javascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
49 truy cập vào dấu kết hợp bị cô lập ◌́javascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
Trong những trường hợp như vậy, hãy áp dụng chuẩn hóa chuỗi. Chuỗi ký tự kết hợp
1 CHỮ NHỎ LATIN E +javascript
const str = 'My face \uD83D\uDE00';
console.log[str]; // => 'My face 😀'
32 KẾT HỢP ACCENT ACUTE có một chuẩn tương đươngjavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
52 CHỮ E NHỎ LATIN VỚI ACUTEjavascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
53javascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
Hãy cải thiện mẫu mã trước đó
3javascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
Thật không may, không phải tất cả các chuỗi ký tự kết hợp đều có các ký tự chính tắc tương đương như một ký hiệu duy nhất. Vì vậy, giải pháp chuẩn hóa không phổ biến
May mắn thay, nó sẽ hoạt động trong hầu hết các trường hợp đối với ngôn ngữ Châu Âu/Bắc Mỹ
3. 5 Kết hợp biểu thức chính quy
Biểu thức chính quy, cũng như chuỗi, hoạt động theo đơn vị mã. Tương tự như các tình huống được mô tả trước đây, điều này tạo ra khó khăn khi xử lý các cặp thay thế và kết hợp các chuỗi ký tự bằng biểu thức chính quy
Các ký tự BMP khớp như mong đợi vì một đơn vị mã đại diện cho một ký hiệu
4javascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
54 có 3 ký hiệu được mã hóa bằng 3 đơn vị mã. Cụm từ thông dụngjavascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
55, dự kiến có 3 đơn vị mã, khớp vớijavascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
54javascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
Khi khớp các biểu tượng thiên thể [được mã hóa bằng một cặp thay thế gồm 2 đơn vị mã], bạn có thể gặp khó khăn
5javascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
24 chứa biểu tượng vũ trụjavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
9 MẶT CƯỜI NHAU.javascript
const str = 'Funny cat \u{1F639}';
console.log[str]; // => 'Funny cat 😹'
const reg = /\u{1F639}/u;
console.log[reg.test['Funny cat 😹']]; // => true
9 được mã hóa bằng cách sử dụng cặp thay thếjavascript
const str = 'Funny cat \u{1F639}';
console.log[str]; // => 'Funny cat 😹'
const reg = /\u{1F639}/u;
console.log[reg.test['Funny cat 😹']]; // => true
8+javascript
const niceEmoticon = '\u{1F607}';
console.log[niceEmoticon]; // => '😇'
const spNiceEmoticon = '\uD83D\uDE07'
console.log[spNiceEmoticon]; // => '😇'
console.log[niceEmoticon === spNiceEmoticon]; // => true
9javascript
const niceEmoticon = '\u{1F607}';
console.log[niceEmoticon]; // => '😇'
const spNiceEmoticon = '\uD83D\uDE07'
console.log[spNiceEmoticon]; // => '😇'
console.log[niceEmoticon === spNiceEmoticon]; // => true
Tuy nhiên, biểu thức chính quy
62 mong đợi một đơn vị mã, vì vậy kết quả khớp không thành công.javascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
63 làjavascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
64javascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
Còn tệ hơn khi xác định các lớp nhân vật bằng các biểu tượng thiên văn. JavaScript ném một lỗi
6javascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
Các điểm mã vũ trụ được mã hóa thành các cặp thay thế. Vì vậy, JavaScript đại diện cho biểu thức chính quy bằng cách sử dụng các đơn vị mã
65. Mỗi đơn vị mã được coi là một phần tử riêng biệt trong mẫu, vì vậy biểu thức chính quy bỏ qua khái niệm cặp thay thếjavascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
Phần
66 của lớp ký tự không hợp lệ, vìjavascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
26 lớn hơnjavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
25. Kết quả là, một lỗi được tạo rajavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
cờ biểu thức chính quy u
May mắn thay, ECMAScript 2015 đã giới thiệu một cờ
68 hữu ích, làm cho biểu thức chính quy nhận biết Unicode. Cờ này cho phép xử lý chính xác các biểu tượng thiên vănjavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
Bạn có thể sử dụng chuỗi thoát unicode trong biểu thức chính quy
70. Lối thoát này ngắn hơn so với biểu thị cặp thay thế cao và thay thế thấpjavascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
71javascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
Hãy áp dụng cờ
68 và xem toán tửjavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
73 [bao gồm các bộ định lượngjavascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
74,javascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
75,javascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
76 vàjavascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
77,javascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
78,javascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
79] khớp với biểu tượng thiên văn như thế nàojavascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
7javascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
Biểu thức chính quy
80, nhận biết Unicode nhờ cờjavascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
68, hiện khớp với biểu tượng thiên thể củajavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
8javascript
const str = 'Funny cat \u{1F639}';
console.log[str]; // => 'Funny cat 😹'
const reg = /\u{1F639}/u;
console.log[reg.test['Funny cat 😹']]; // => true
Cờ
68 cũng cho phép xử lý chính xác các biểu tượng thiên văn trong các lớp ký tựjavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
8javascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
84 hiện được đánh giá là một loạt các biểu tượng thiên văn.javascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
85 trận đấujavascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
30javascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
Biểu thức chính quy và dấu kết hợp
Thật không may, với cờ
68 hoặc không có nó, biểu thức chính quy coi các dấu kết hợp là các đơn vị mã riêng biệtjavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
Nếu bạn cần khớp một chuỗi ký tự kết hợp, bạn phải khớp riêng ký tự cơ sở và dấu kết hợp
Hãy xem ví dụ sau
9javascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
Chuỗi được hiển thị có 4 ký hiệu
23javascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
Tuy nhiên, biểu thức chính quy khớp với
21 dưới dạng một chuỗi gồm 5 phần tửjavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
90javascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
4. Tóm lược
Có lẽ khái niệm quan trọng nhất về Unicode trong JavaScript là coi các chuỗi là các chuỗi đơn vị mã, vì chúng thực sự là như vậy.
Sự nhầm lẫn xuất hiện khi nhà phát triển nghĩ rằng các chuỗi bao gồm các biểu đồ [hoặc ký hiệu], bỏ qua khái niệm chuỗi đơn vị mã
Nó tạo ra sự hiểu lầm khi xử lý các chuỗi chứa các cặp thay thế hoặc kết hợp các chuỗi ký tự
- Lấy chiều dài chuỗi
- Định vị ký tự
- Kết hợp biểu thức chính quy
Lưu ý rằng hầu hết các phương thức chuỗi trong JavaScript không nhận dạng Unicode. như
91,javascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
38, v.v.javascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
ECMAScript 2015 đã giới thiệu các tính năng hay như trình tự thoát điểm mã
93 trong chuỗi và biểu thức chính quyjavascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
Cờ biểu thức chính quy mới
68 cho phép khớp chuỗi nhận biết Unicode. Nó dễ dàng phù hợp với các biểu tượng thiên vănjavascript
function getSurrogatePair[astralCodePoint] {
let highSurrogate =
Math.floor[[astralCodePoint - 0x10000] / 0x400] + 0xD800;
let lowSurrogate = [astralCodePoint - 0x10000] % 0x400 + 0xDC00;
return [highSurrogate, lowSurrogate];
}
getSurrogatePair[0x1F600]; // => [0xD83D, 0xDE00]
function getAstralCodePoint[highSurrogate, lowSurrogate] {
return [highSurrogate - 0xD800] * 0x400
+ lowSurrogate - 0xDC00 + 0x10000;
}
getAstralCodePoint[0xD83D, 0xDE00]; // => 0x1F600
Trình lặp chuỗi
09 nhận biết Unicode. Bạn có thể sử dụng toán tử trải rộngjavascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
10 hoặcjavascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
11 để tạo một mảng các ký hiệu và tính toán độ dài chuỗi hoặc truy cập các ký tự theo chỉ mục mà không phá vỡ cặp thay thế. Lưu ý rằng các hoạt động này có một số tác động hiệu suấtjavascript
console.log['\u0061\u030A']; // => 'å'
console.log['\u0061']; // => 'a'
Nếu bạn cần một cách tốt hơn để xử lý các ký tự Unicode, bạn có thể sử dụng thư viện punycode hoặc tạo các biểu thức chính quy chuyên dụng
Tôi hy vọng bài viết đã giúp bạn thành thạo Unicode
Bạn có biết các sắc thái Unicode thú vị khác trong JavaScript không?
Thích bài viết?
đề xuất cải tiến
Bài đăng chất lượng vào hộp thư đến của bạn
Tôi thường xuyên xuất bản bài viết có chứa
- Các khái niệm JavaScript quan trọng được giải thích bằng các từ đơn giản
- Tổng quan về các tính năng JavaScript mới
- Cách sử dụng TypeScript và cách gõ
- Thiết kế phần mềm và thực hành mã hóa tốt
Đăng ký nhận bản tin của tôi để nhận chúng ngay trong hộp thư đến của bạn
Đặt muaTham gia cùng 6861 người đăng ký khác
Giới thiệu về Dmitri Pavlutin
Nhà văn và huấn luyện viên công nghệ. Thói quen hàng ngày của tôi bao gồm [nhưng không giới hạn] uống cà phê, viết mã, viết, huấn luyện, vượt qua sự nhàm chán 😉
Hiện đang làm việc với tư cách là nhà phát triển nguồn mở toàn thời gian trong dự án Readapt - nhằm mục đích làm cho mọi người đều có thể truy cập web