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

116
Visualizações
problem with ".match(/\w+/g).length" in react

Using .match(/\w+/g).length while counting the number of words, returns an error if space is given at beginning of a sentence. How do i make it error-free?

The error says "can't read the properties of null (reading 'length')"

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

0

match returns null when no matches were found. In your case, it is when there are no words (empty strings or just white spaces). In other cases, it is an array. You should check if the result of the match is null before checking the .length.

One way to overcome this error, suppose str is the string you are checking:

const matchStr = str.match(/\w+/g) || []; // The || [] will give you empty array instead of null when no matches were found
matchStr.length // now you can safely use .length property, because you will always have an array.
about 4 years ago · Juan Pablo Isaza Relatório

0

Try this str.match(/(\w+)/g).length

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