Arth
Arth

Reputation: 361

Splitting array with more digits

Lets assume I have following string:

(255+23*452-2)

Then I use

string.split("")

so I get an array. But problem is that I only get array like this: [2,5,5,+2,3,*...] which I dont need. I need to get array like this: [255, +, 23, *, 452, -, 2, )...etc]

Thanks for advice

Upvotes: 1

Views: 53

Answers (1)

Robo Robok
Robo Robok

Reputation: 22673

Match everything that is either a group of digits, or not digit at all:

const string = "(255+23*452-2)";
const components = string.match(/\d+|\D/g);
// ["(", "255", "+", "23", "*", "452", "-", "2", ")"]

Upvotes: 4

Related Questions