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

120
Views
problema con ".match(/\w+/g).length" en reaccionar

Usar .match(/\w+/g).length mientras se cuenta el número de palabras, devuelve un error si se da un espacio al principio de una oración. ¿Cómo lo hago sin errores?

El error dice "no se pueden leer las propiedades de nulo (leyendo 'longitud')"

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

match devuelve null cuando no se encuentran coincidencias. En tu caso, es cuando no hay palabras (cadenas vacías o solo espacios en blanco). En otros casos, es una matriz. Debe verificar si el resultado de la coincidencia es null antes de verificar .length .

Una forma de superar este error, supongamos que str es la cadena que está comprobando:

 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 Report

0

Pruebe este str.match(/(\w+)/g).length

about 4 years ago · Juan Pablo Isaza 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!