Given array:
const array = ["string1234","stringNew264"]
Result arrays should be like below arrays:
stringArray: ["string","stringNew"]
numbersArray: [1234,264]
I tried using split()
, join()
, foreach
loop method but not getting proper result.
3
Answers
You can use reduce method and regex to select according parts.
Here is one approach. We can first split each array entry on
(?<=D)(?=d)
, which will match the boundary between string and number. Then, usemap()
on the resulting array of arrays to generate the desired output.Maybe I went a little over the top here, but OP did not clearly specify in which order or how many strings and numbers can be expected in the array elements, I put together an alternative solution here that will collect all strings and all numbers from the array elements: