JavaScript Unicode, Bytes, and Advanced Regular Expressions (Summary)

Congratulations, you made it to the end of the course! The payments team’s branch name helper now runs in JavaScript, matching the team lead’s Python. No cut splits the 😀 in Zoë’s title, the two café titles for PAY-2402 share one branch, and the Tokyo name fits the host’s 32-byte limit.

Here are the three problems again, measured with the course’s tools:

Language: JavaScript
const title = "Zoë's 😀 refund fix";
console.log(title.length, [...title].length);
console.log([...title].slice(0, 7).join(""));

const typed = "cafe\u0301 receipts";
console.log(typed === "café receipts", typed.normalize() === "café receipts");

const branch = "pay-2403-東京-refund-approval";
console.log(branch.length, new TextEncoder().encode(branch).length);

Two code units make one code point, a cut by code point keeps 😀 whole, normalizing makes the two café titles equal, and the host counts 31 bytes, not 27:

Language: Program Output
19 18
Zoë's 😀
false true
27 31

In Python, len(title) gives 18, title[:7] keeps the emoji, the NFC-normalized title compares True, and len() of the encoded bytes gives 31.

In this course, you learned how to:

  • Tell code units from code points and walk a string by code point with for...of and .codePointAt()
  • Compare canonically equivalent text with .normalize() and count what a reader sees with Intl.Segmenter
  • Encode text to UTF-8 bytes with TextEncoder and work with Uint8Array views and copies
  • Match Unicode with the u and v flags, look behind and ahead, and scan with the y flag
  • Rebuild the team’s branch name helper in JavaScript, matching the Python version

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.