Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

195
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda