Chuyển đổi UTF8 sang Unicode JavaScript

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

javascript

console.log['cafe\u0301']; // => 'café'

console.log['café']; // => 'café'

0 cho User2

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

javascript

console.log['cafe\u0301']; // => 'café'

console.log['café']; // => 'café'

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 User2

Khi máy tính của Người dùng 2 nhận được dãy số

javascript

console.log['cafe\u0301']; // => 'café'

console.log['café']; // => 'café'

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é'

0

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,

javascript

console.log['cafe\u0301']; // => 'café'

console.log['café']; // => 'café'

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 ứng

javascript

console.log['cafe\u0301']; // => 'café'

console.log['café']; // => 'café'

4, là một điểm mã trong ký hiệu

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

1

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

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 đề cập đến

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à

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

3

Đ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ừ

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

4 đến

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

5

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

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 str = '\x4A\x61vaScript';

console.log[str]; // => 'JavaScript'

const reg = /\x4A\x61va.*/;

console.log[reg.test['JavaScript']]; // => true

0 là các điểm mã

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ụ:

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 tương ứng với ký tự có tên CHỮ VỐN LATIN A [được hiển thị là

javascript

const str = '\x4A\x61vaScript';

console.log[str]; // => 'JavaScript'

const reg = /\x4A\x61va.*/;

console.log[reg.test['JavaScript']]; // => true

2] hoặc

javascript

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]

Không phải tất cả các điểm mã đều có các ký tự liên quan. Có sẵn

javascript

const str = '\x4A\x61vaScript';

console.log[str]; // => 'JavaScript'

const reg = /\x4A\x61va.*/;

console.log[reg.test['JavaScript']]; // => true

5 điểm mã [phạm vi

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

4 đến

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

5], nhưng chỉ

javascript

const str = '\x4A\x61vaScript';

console.log[str]; // => 'JavaScript'

const reg = /\x4A\x61va.*/;

console.log[reg.test['JavaScript']]; // => true

8 [kể từ tháng 9 năm 2021] có các ký tự liên quan

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ừ

javascript

const str = '\x4A\x61vaScript';

console.log[str]; // => 'JavaScript'

const reg = /\x4A\x61va.*/;

console.log[reg.test['JavaScript']]; // => true

9 đến

javascript

const str = 'I\u0020learn \u0055nicode';

console.log[str]; // => 'I learn Unicode'

const reg = /\u0055ni.*/;

console.log[reg.test['Unicode']]; // => 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 1016

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ừ

    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

    4 đến

    javascript

    const str = 'I\u0020learn \u0055nicode';

    console.log[str]; // => 'I learn Unicode'

    const reg = /\u0055ni.*/;

    console.log[reg.test['Unicode']]; // => true

    3,
  • Mặt phẳng 1 chứa các điểm mã từ

    javascript

    const str = 'I\u0020learn \u0055nicode';

    console.log[str]; // => 'I learn Unicode'

    const reg = /\u0055ni.*/;

    console.log[reg.test['Unicode']]; // => true

    4 đến

    javascript

    const str = 'I\u0020learn \u0055nicode';

    console.log[str]; // => 'I learn Unicode'

    const reg = /\u0055ni.*/;

    console.log[reg.test['Unicode']]; // => true

    5
  • ...
  • Mặt phẳng 16 chứa các điểm mã từ

    javascript

    const str = 'I\u0020learn \u0055nicode';

    console.log[str]; // => 'I learn Unicode'

    const reg = /\u0055ni.*/;

    console.log[reg.test['Unicode']]; // => true

    6 đến

    javascript

    const str = 'I\u0020learn \u0055nicode';

    console.log[str]; // => 'I learn Unicode'

    const reg = /\u0055ni.*/;

    console.log[reg.test['Unicode']]; // => true

    7

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ừ

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

4 đến

javascript

const str = 'I\u0020learn \u0055nicode';

console.log[str]; // => 'I learn Unicode'

const reg = /\u0055ni.*/;

console.log[reg.test['Unicode']]; // => true

3 và có thể có tối đa 4 chữ số thập lục phân

Nhà phát triển thường xử lý các ký tự từ BMP

Một số nhân vật từ BMP

  • javascript

    console.log['cafe\u0301']; // => 'café'

    console.log['café']; // => 'café'

    5 là

    javascript

    const str = 'My face \uD83D\uDE00';

    console.log[str]; // => 'My face 😀'

    1 có tên là CHỮ NHỎ LATIN E
  • javascript

    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 ĐỨNG
  • javascript

    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 ĐEN
  • javascript

    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à UMBRELLA

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ừ

javascript

const str = 'My face \uD83D\uDE00';

console.log[str]; // => 'My face 😀'

8 đến

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

5

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.

javascript

const str = 'Funny cat \u{1F639}';

console.log[str]; // => 'Funny cat 😹'

const reg = /\u{1F639}/u;

console.log[reg.test['Funny cat 😹']]; // => true

0 hoặc

javascript

const str = 'Funny cat \u{1F639}';

console.log[str]; // => 'Funny cat 😹'

const reg = /\u{1F639}/u;

console.log[reg.test['Funny cat 😹']]; // => true

1

Chúng ta hãy xem một số nhân vật từ máy bay astral

  • javascript

    const str = 'Funny cat \u{1F639}';

    console.log[str]; // => 'Funny cat 😹'

    const reg = /\u{1F639}/u;

    console.log[reg.test['Funny cat 😹']]; // => true

    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 CLEF
  • javascript

    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 B
  • javascript

    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-04
  • 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 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ƯỜI

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ự

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

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 niceEmoticon = '\u{1F607}';

console.log[niceEmoticon]; // => '😇'

const spNiceEmoticon = '\uD83D\uDE07'

console.log[spNiceEmoticon]; // => '😇'

console.log[niceEmoticon === spNiceEmoticon]; // => true

1

Bây giờ, hãy hỏi mã hóa UTF-16 nên chuyển đổi

javascript

const niceEmoticon = '\u{1F607}';

console.log[niceEmoticon]; // => '😇'

const spNiceEmoticon = '\uD83D\uDE07'

console.log[spNiceEmoticon]; // => '😇'

console.log[niceEmoticon === spNiceEmoticon]; // => true

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

4

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

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. Ký tự này ánh xạ tới

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 điểm mã, từ một cõi trung giới

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ã

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 chia thành cái gọi là cặp thay thế.

javascript

const niceEmoticon = '\u{1F607}';

console.log[niceEmoticon]; // => '😇'

const spNiceEmoticon = '\uD83D\uDE07'

console.log[spNiceEmoticon]; // => '😇'

console.log[niceEmoticon === spNiceEmoticon]; // => 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]

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

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 [

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

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

02

javascript

console.log['\uD83D\uDE00']; // => '😀'

Đơn vị mã thay thế cao nhận các giá trị từ phạm vi

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

03 đế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

04. Đơn vị mã thay thế thấp nhận các giá trị từ phạm vi

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

05 đế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

06

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ụ:

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

07 CHỮ NHỎ LATIN F là một cách viết tiếng Anh

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

08

Có những trường hợp khi một biểu đồ chứa một chuỗi các ký tự

Ví dụ,

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

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ụng

javascript

const niceEmoticon = '\u{1F607}';

console.log[niceEmoticon]; // => '😇'

const spNiceEmoticon = '\uD83D\uDE07'

console.log[spNiceEmoticon]; // => '😇'

console.log[niceEmoticon === spNiceEmoticon]; // => true

1 CHỮ NHỎ 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

3] kết hợp với một ký tự đặc biệ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

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ợp

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.

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

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

const niceEmoticon = '\u{1F607}';

console.log[niceEmoticon]; // => '😇'

const spNiceEmoticon = '\uD83D\uDE07'

console.log[spNiceEmoticon]; // => '😇'

console.log[niceEmoticon === spNiceEmoticon]; // => true

1 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

12 được sử dụng để xây dựng

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

09

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ừ

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

4 đến

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

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]

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é'

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

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ệu

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

23

Độ 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

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

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ê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

27 ước tính thành

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

28. Ngay cả khi kết xuấ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

24 chỉ có một biểu tượng

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

30

Kịch bản tương tự cũng xảy ra với chuỗi

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. Dấu kết hợp

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

32 áp dụng cho ký tự trước đó và kết quả hiển thị là một ký hiệu

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

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ứa

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

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

28

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

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

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.

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.

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

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ân

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 với độ dài cố định là 2 chữ số
Ví dụ:

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

43 [ký hiệu

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

44] hoặc

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

45 [ký hiệu

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

46]

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ừ

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

47 đế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

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ắn

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à

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, 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ân

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 với độ dài cố định là 4 chữ số. Ví dụ:

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

52 [ký hiệu

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

53] hoặc

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

54 [ký hiệu tích phâ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

55]

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ừ

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

4 đến

javascript

const str = 'I\u0020learn \u0055nicode';

console.log[str]; // => 'I learn Unicode'

const reg = /\u0055ni.*/;

console.log[reg.test['Unicode']]; // => true

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ụng

Để 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 😀'

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

58 là một cặp thay thế được tạo bằng 2 chuỗi thoát

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.

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

4 đến

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

5, tôi. e. BMP và máy bay vũ trụ

Định dạng mới được gọi là trình tự thoát đ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

61, 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

8 là số thập lục phân có độ dài thay đổi từ 1 đến 6 chữ số

Ví dụ:

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

63 [ký hiệu

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

64] hoặc

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

65 [ký hiệu chú mèo ngộ nghĩnh

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

66]

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

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

67 có một cờ đặc biệ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

68, cho phép các tính năng Unicode bổ sung [xem thêm về điều đó trong]

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

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

69 MẶT CƯỜI VỚI điểm mã HALO

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

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

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ã astral

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

69

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ã].

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 [đượ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ới

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

70

Khi một biểu thức chính quy được tạo bằng cách sử dụng hàm tạo

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

76, trong chuỗi ký tự, bạn phải thay thế từng

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

77 bằng

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

78 để biểu thị thoát unicode. Các đối tượng biểu thức chính quy sau đây là tương đương

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

0

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"

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

Các chuỗi

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

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 nhau

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

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

2

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

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 nhau
Nó xảy ra bởi vì ________ 72 ______ 83 graphene có thể được cấu tạo theo hai cách

  • Sử dụng

    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

    84 CHỮ C NHỎ LATIN VỚI CEDILLA
  • Hoặc sử dụng một chuỗi ký tự kết hợp.

    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

    85 CHỮ C CHỮ NHỎ LATIN cộng với dấu kết hợp

    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

    86 CEDILLA KẾT HỢP

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

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

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 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

    89 dưới dạng Chuẩn hóa Hình thức Thành phần Chính tắc
  • 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

    91 dưới dạng Chuẩn hóa Phân rã Canonical
  • 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

    92 dưới dạng Thành phần Tương thích Biểu mẫu Chuẩn hóa
  • 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

    93 dưới dạng Phân rã tương thích biểu mẫu chuẩn hóa

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

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

Khi

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

94 được gọi, một phiên bản chính tắc của

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 được trả về [_______72_______96 được thay thế bằng

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

97]. Vì vậy, phép so sánh

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

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 đợi

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

81 không bị ảnh hưởng bởi quá trình chuẩn hóa, vì nó đã ở dạng chính tắc

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

javascript

console.log['\u0061\u030A']; // => 'å'

console.log['\u0061']; // => 'a'

01. Thuộc tính này cho biết số đơn vị mã mà một chuỗi có

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

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

4

Mỗi đơn vị mã trong

javascript

console.log['\u0061\u030A']; // => 'å'

console.log['\u0061']; // => 'a'

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'

03

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ụ

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

5

Khi chuỗi

javascript

console.log['\u0061\u030A']; // => 'å'

console.log['\u0061']; // => 'a'

04 được hiển thị, nó chứa 4 ký hiệu

javascript

console.log['\u0061\u030A']; // => 'å'

console.log['\u0061']; // => 'a'

05

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

27 đánh giá thành

javascript

console.log['\u0061\u030A']; // => 'å'

console.log['\u0061']; // => 'a'

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ế]

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

javascript

console.log['\u0061\u030A']; // => 'å'

console.log['\u0061']; // => 'a'

09. Bạn có thể kết hợp chuỗi với toán tử trải rộng hàm

javascript

console.log['\u0061\u030A']; // => 'å'

console.log['\u0061']; // => 'a'

10 hoặc hàm

javascript

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ề

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

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

6

javascript

console.log['\u0061\u030A']; // => 'å'

console.log['\u0061']; // => 'a'

10 tạo một mảng gồm 4 ký hiệu. Cặp thay thế mã hóa

javascript

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 Unicode

Độ 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ử

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

7

Chuỗi

javascript

console.log['\u0061\u030A']; // => 'å'

console.log['\u0061']; // => 'a'

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ệu

Khi chuẩn hóa

javascript

console.log['\u0061\u030A']; // => 'å'

console.log['\u0061']; // => 'a'

14, may mắn là chuỗi ký tự kết hợp

javascript

console.log['\u0061\u030A']; // => 'å'

console.log['\u0061']; // => 'a'

18 có dạng chính tắc

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

33. Vì vậy,

javascript

console.log['\u0061\u030A']; // => 'å'

console.log['\u0061']; // => 'a'

20 chứa các ký hiệu

javascript

console.log['\u0061\u030A']; // => 'å'

console.log['\u0061']; // => 'a'

21 dự kiến

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

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

javascript

console.log['\u0061\u030A']; // => 'å'

console.log['\u0061']; // => 'a'

14 có 6 đơn vị mã và

javascript

console.log['\u0061\u030A']; // => 'å'

console.log['\u0061']; // => 'a'

15 đánh giá thành

javascript

console.log['\u0061\u030A']; // => 'å'

console.log['\u0061']; // => 'a'

24. Tuy nhiên

javascript

console.log['\u0061\u030A']; // => 'å'

console.log['\u0061']; // => 'a'

14 có 4 ký hiệu

Quá trình chuẩn hóa

javascript

console.log['\u0061\u030A']; // => 'å'

console.log['\u0061']; // => 'a'

26 biến đổi chuỗi kết hợp

javascript

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]

Đáng buồn thay,

javascript

console.log['\u0061\u030A']; // => 'å'

console.log['\u0061']; // => 'a'

20 đánh giá thành

javascript

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 đợi

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

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ỗ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

javascript

console.log['\u0061\u030A']; // => 'å'

console.log['\u0061']; // => 'a'

0

Bởi vì

javascript

console.log['\u0061\u030A']; // => 'å'

console.log['\u0061']; // => 'a'

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ế

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

    javascript

    console.log['\u0061\u030A']; // => 'å'

    console.log['\u0061']; // => 'a'

    34
  • Nhận số điểm mã bằng cách sử dụng

    javascript

    console.log['\u0061\u030A']; // => 'å'

    console.log['\u0061']; // => 'a'

    35, sau đó chuyển đổi số thành ký hiệu bằng cách sử dụng

    javascript

    console.log['\u0061\u030A']; // => 'å'

    console.log['\u0061']; // => 'a'

    36 [tùy chọn được đề xuất]

Hãy áp dụng cả hai tùy chọn

javascript

console.log['\u0061\u030A']; // => 'å'

console.log['\u0061']; // => 'a'

1

javascript

console.log['\u0061\u030A']; // => 'å'

console.log['\u0061']; // => 'a'

37 trả về một mảng ký hiệu mà chuỗi

javascript

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'

40

Cuộc gọi phương thức

javascript

console.log['\u0061\u030A']; // => 'å'

console.log['\u0061']; // => 'a'

41 nhận biết Unicode, vì vậy nó trả về số điểm mã astral

javascript

console.log['\u0061\u030A']; // => 'å'

console.log['\u0061']; // => 'a'

42 của ký tự đầu tiên trong chuỗi

javascript

console.log['\u0061\u030A']; // => 'å'

console.log['\u0061']; // => 'a'

38. Hàm

javascript

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'

40

Đị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 đề

javascript

console.log['\u0061\u030A']; // => 'å'

console.log['\u0061']; // => 'a'

2

javascript

console.log['\u0061\u030A']; // => 'å'

console.log['\u0061']; // => 'a'

46 chỉ truy cập ký tự cơ sở

javascript

console.log['cafe\u0301']; // => 'café'

console.log['café']; // => 'café'

5, không có dấu kết hợp

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

32 DẤU MẮT KẾT HỢP [được hiển thị là ◌́ ]

javascript

console.log['\u0061\u030A']; // => 'å'

console.log['\u0061']; // => 'a'

49 truy cập vào dấu kết hợp bị cô lập ◌́

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

javascript

const str = 'My face \uD83D\uDE00';

console.log[str]; // => 'My face 😀'

1 CHỮ NHỎ LATIN E +

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

32 KẾT HỢP ACCENT ACUTE có một chuẩn tương đương

javascript

console.log['\u0061\u030A']; // => 'å'

console.log['\u0061']; // => 'a'

52 CHỮ E NHỎ LATIN VỚI ACUTE

javascript

console.log['\u0061\u030A']; // => 'å'

console.log['\u0061']; // => 'a'

53

Hãy cải thiện mẫu mã trước đó

javascript

console.log['\u0061\u030A']; // => 'å'

console.log['\u0061']; // => 'a'

3

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

javascript

console.log['\u0061\u030A']; // => 'å'

console.log['\u0061']; // => 'a'

4

javascript

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ụng

javascript

console.log['\u0061\u030A']; // => 'å'

console.log['\u0061']; // => 'a'

55, dự kiến ​​có 3 đơn vị mã, khớp với

javascript

console.log['\u0061\u030A']; // => 'å'

console.log['\u0061']; // => 'a'

54

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

javascript

console.log['\u0061\u030A']; // => 'å'

console.log['\u0061']; // => 'a'

5

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

24 chứa biểu tượng vũ trụ

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 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 niceEmoticon = '\u{1F607}';

console.log[niceEmoticon]; // => '😇'

const spNiceEmoticon = '\uD83D\uDE07'

console.log[spNiceEmoticon]; // => '😇'

console.log[niceEmoticon === spNiceEmoticon]; // => true

8+

javascript

const niceEmoticon = '\u{1F607}';

console.log[niceEmoticon]; // => '😇'

const spNiceEmoticon = '\uD83D\uDE07'

console.log[spNiceEmoticon]; // => '😇'

console.log[niceEmoticon === spNiceEmoticon]; // => true

9

Tuy nhiên, biểu thức chính quy

javascript

console.log['\u0061\u030A']; // => 'å'

console.log['\u0061']; // => 'a'

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'

64

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

javascript

console.log['\u0061\u030A']; // => 'å'

console.log['\u0061']; // => 'a'

6

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ã

javascript

console.log['\u0061\u030A']; // => 'å'

console.log['\u0061']; // => 'a'

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ế

Phần

javascript

console.log['\u0061\u030A']; // => 'å'

console.log['\u0061']; // => 'a'

66 của lớp ký tự không hợp lệ, 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 lớn hơ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

25. Kết quả là, một lỗi được tạo ra

cờ biểu thức chính quy u

May mắn thay, ECMAScript 2015 đã giới thiệu một cờ

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

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ăn

Bạn có thể sử dụng chuỗi thoát unicode trong biểu thức chính quy

javascript

console.log['\u0061\u030A']; // => 'å'

console.log['\u0061']; // => 'a'

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ấp

javascript

console.log['\u0061\u030A']; // => 'å'

console.log['\u0061']; // => 'a'

71

Hãy áp dụng cờ

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

68 và xem toán tử

javascript

console.log['\u0061\u030A']; // => 'å'

console.log['\u0061']; // => 'a'

73 [bao gồm các bộ định lượng

javascript

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ào

javascript

console.log['\u0061\u030A']; // => 'å'

console.log['\u0061']; // => 'a'

7

Biểu thức chính quy

javascript

console.log['\u0061\u030A']; // => 'å'

console.log['\u0061']; // => 'a'

80, nhận biết Unicode nhờ cờ

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

68, hiện khớp với biểu tượng thiên thể của

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

Cờ

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

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

console.log['\u0061\u030A']; // => 'å'

console.log['\u0061']; // => 'a'

8

javascript

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 đấu

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

30

Biểu thức chính quy và dấu kết hợp

Thật không may, với cờ

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

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ệt

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

javascript

console.log['\u0061\u030A']; // => 'å'

console.log['\u0061']; // => 'a'

9

Chuỗi được hiển thị có 4 ký hiệu

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

23

Tuy nhiên, biểu thức chính quy khớp với

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

21 dưới dạng một chuỗi gồm 5 phần tử

javascript

console.log['\u0061\u030A']; // => 'å'

console.log['\u0061']; // => 'a'

90

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ư

javascript

console.log['\u0061\u030A']; // => 'å'

console.log['\u0061']; // => 'a'

91,

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

38, v.v.

ECMAScript 2015 đã giới thiệu các tính năng hay như trình tự thoát điểm mã

javascript

console.log['\u0061\u030A']; // => 'å'

console.log['\u0061']; // => 'a'

93 trong chuỗi và biểu thức chính quy

Cờ biểu thức chính quy mới

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

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ăn

Trình lặp chuỗi

javascript

console.log['\u0061\u030A']; // => 'å'

console.log['\u0061']; // => 'a'

09 nhận biết Unicode. Bạn có thể sử dụng toán tử trải rộng

javascript

console.log['\u0061\u030A']; // => 'å'

console.log['\u0061']; // => 'a'

10 hoặc

javascript

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ất

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 mua

Tham 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

Cách chuyển đổi UTF

The way to solve it would be to add an appropriate meta tag like and XML declaration like .

Cách chuyển đổi UTF

chuyển đổi một chuỗi từ UTF-8 sang ASCII hoặc ANSI . utf8 = Encoding. UTF8; . ASCII; .

Làm cách nào để viết các ký tự Unicode trong JavaScript?

Trong Javascript, mã định danh và chuỗi ký tự có thể được biểu thị bằng Unicode thông qua chuỗi thoát Unicode. Cú pháp chung là \uXXXX , trong đó X biểu thị bốn chữ số thập lục phân . Ví dụ: chữ o được ký hiệu là '\u006F' trong Unicode.

Làm cách nào để chuyển đổi Unicode thành văn bản trong JavaScript?

phương thức fromCharCode[] chuyển đổi giá trị Unicode thành ký tự. chuỗi. fromCharCode[] là một phương thức tĩnh của đối tượng String. Cú pháp luôn là Chuỗi.

Chủ Đề