1

Maybe there is solution righ in front of my nose but i cant see it.

var expr = "-5+6.3x24";
console.log(expr.split(/([+\-x\/])/));

Output is logically:

["", "-", "5", "+", "6.3", "x", "24"]

But what i need is following:

["-5", "+", "6.3", "x", "24"]

I need to split this string by separators +,-,x,/, but not when is the separator at the beginning of string. Any ideas how to edit this code? Thanks alot.

1
  • expr.match(/-?\d+(?:\.\d+)?|\S/g) Commented Jul 17, 2016 at 17:09

1 Answer 1

3
var expr = "-5+6.3x24";
console.log(expr.split(/(?!^)([+\-x\/])/));
Sign up to request clarification or add additional context in comments.

3 Comments

This solution will produce the appropriate result for that specific example. However, it will probably give an undesired result for other examples like "5x-5" that gives ["-5", "x", "", "-", "5"].
True, but there is no reason to have such syntax, this is not a valid mathematical syntax.
Thank you very much. It is working marvelously. Example like "5x-5" is in my case evaluated like err.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.