Array mit mehr Ziffern teilen

Oct 07 2020

Nehmen wir an, ich habe folgende Zeichenfolge:

(255+23*452-2)

Dann benutze ich

string.split("")

Also bekomme ich ein Array. Aber das Problem ist, dass ich nur ein Array wie dieses bekomme: [2,5,5,+2,3,*...]das ich nicht brauche. Ich muss Array wie folgt bekommen:[255, +, 23, *, 452, -, 2, )...etc]

Danke für den Rat

Antworten

4 RoboRobok Oct 07 2020 at 17:38

Ordnen Sie alles zu, was entweder eine Gruppe von Ziffern oder gar keine Ziffer ist:

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