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

177
Vistas
javascript split by whitespace only works with regex

I have an string of the format

let a = "A B"

where there is a whitespace between the first part and the second.

When I try a.split(" "), it returns ["A B"], which is a array of length 1, containing the original string.

When I try a.split(/(\s+)/), I get the desired result, ["A", "B"]

The wierd thing is split(" ") only fails sometimes, in other times it works same as expected, I tried a couple of input but did not figure out the reason behind.

Does anyone have this problem before? This looks super weird.

UPDATE:

Sorry, I think oversimply the problem, so the string comes from innerText of a DOM object. Its original look is of the format {var1} {var2}

Is this where the problem comes from? Because I cannot find any other explanation, but this one still does not makes sense to me....

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