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

179
Visualizações
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 à 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