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

173
Views
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
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!