Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

196
Views
How to use substring with special unicode characters?

var string = "abc𝑚";
var lastchar = string.substr(string.length - 1);
console.log(lastchar);

This returns ? instead of 𝑚

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

In JavaScript, a string is a series of UTF-16 code units (details in my blog post What is a string?). In UTF-16, that last glyph (loosely, "character') requires two code units (which combine to make a single code point), so your string length is 5.

Until ES2015 there wasn't much built into JavaScript to help you with this, But when iterability was introduced, strings were made iterable and they iterate over their code points, not code units. Spread operations use iteration, so you can spread that string out into an array to get at its code points:

const string = "abc𝑚";
console.log(string.length); // 5
const chars = [...string];
console.log(chars.length);  // 4
const lastchar = chars.slice(chars.length - 1).join("");
console.log(lastchar);

That's just an example to demonstrate the distinction and how you can use code points fairly easily.

Even code points aren't necessarily glyphs because some code points combine with other ones to form a single glyph. (For instance, in Devanagari, the word for the language is "देवनागरी" which looks like five glyphs to native readers, but is eight code points because some of them are written with a base syllable glyph modified by a vowel code point after.) There's a new Intl.Segmenter under development that would help with those situations as well.

about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!