Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

109
Visualizações
Is there a way to know the number of characters in a string?

This question concerns unicode characters that are more than one utf-16 character

string.length returns the number of unicode 16 chars in the string. But what about the case of characters that are more than 1 unicode 16 character? Is it possible to get the actual number of characters as well as "true" values from string.charAt of the actual character, and not the utf-16 pieces?

var test = "🀄";
console.log(test.length)
var regex = /[\u{1F000}-\u{1F0FF}]/g;
var regex2 = /🀄/g
console.log('test: ' + regex.test(test));
console.log('test2: ' + regex2.test(test));

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Another option is to use RegExp with the unicode flag.

var test = "🀄";
var length = test.match(/./ug).length;
console.log(length);

Please note that this and the answer using the spread operator can break under certain circumstances.

var test = "Z͑ͫ̓ͪ̂ͫ̽͏̴̙̤̞͉͚̯̞̠͍A̴̵̜̰͔ͫ͗͢L̠ͨͧͩ͘G̴̻͈͍͔̹̑͗̎̅͛́Ǫ̵̹̻̝̳͂̌̌͘";
var length = test.match(/./ug).length;
console.log(length);

To get the char at a specific index you can use the following function.

var test = "🀄👼👺💩";

var charAt = charAt(test);

console.log(charAt);

function charAt(string, index) {
    if(!index)
        index = 0;

    return string.match(/./ug)[index];
}

about 4 years ago · Juan Pablo Isaza Relatório

0

you can use spread operator:

var test = "🀄";
var test2 = [...test];
console.log(test.length);
console.log(test2.length);

about your second question, you need to modify regexp:

var test = "🀄";
console.log(test.length)
var regex = /[\u{1F000}-\u{1F0FF}]/ug;
var regex2 = /🀄/g
console.log('test: ' + regex.test(test));
console.log('test2: ' + regex2.test(test));

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda