-1

I was wondering what Regular Expression I could use to split a string into an array if the string looked like the following:

2x+5=7y^2+x^2+5

would come into an array like:

[2x,+5,=,7y^2,+x^2,+5]

If that didn't make sense. Split on any value including - or + followed by a letter followed by or not followed by a carret and a number followed by a letter. I am asking specifically what formula in Regex I could use

Thank you.

Tartarus13
  • 59
  • 6

1 Answers1

0

You could use match with the following regex:

var s = "2x+5=7y^2+x^2+5",
    res = s.match(/=|[+-]?\d*(?:\w(?:\^\d+)?|\d+)/g);
console.log(res);
trincot
  • 317,000
  • 35
  • 244
  • 286