1

I want to know an easy way of checking if a string is a mathematical expression. As an example, Google (and other search engines) use this when you search.

PS: I don't necessarily want to check stuff like sin, cos, etc.

Example: 2+2 should return true but letters+somethingelse should return false

Thanks!

3
  • 2
    Can you post some code? Commented Feb 27, 2018 at 18:45
  • maybe try converting the result to int, if that fails return false. Commented Feb 27, 2018 at 18:47
  • @void I have a string, str, equal to an input's value through document.getElementById Commented Feb 27, 2018 at 18:48

5 Answers 5

7

Here's a regex that can do what I think you're looking for:

/(?:(?:^|[-+_*/])(?:\s*-?\d+(\.\d+)?(?:[eE][+-]?\d+)?\s*))+$/

https://regex101.com/r/w74GSk/4

It matches a number, optionally negative, with an optional decimal number followed by zero or more operator/number pairs.

It also allows for whitespace between numbers and operators.

const re = /(?:(?:^|[-+_*/])(?:\s*-?\d+(\.\d+)?(?:[eE][+-]?\d+)?\s*))+$/;

function test(s) {
  console.log("%s is valid? %s", s, re.test(s));
}

// valid
test(" 1 ");
test("1 + 2");
test(" 1 * 2 * 3 ");
test("-1 * 2 - -3");
test("-1 * 2 - -3e4");
test("-1 * 2 - -3.5E6");

// invalid
test("1 +");
test("1 + foo");

This may need to be expanded, based on what you want to allow. One thing it does not handle is parentheses to override operator precedence.

Sign up to request clarification or add additional context in comments.

5 Comments

not that the question is asking for it but for anyone else, this doesn't account for the number e so beware of that edge case
@floor: You're right. There's a good bit that this doesn't handle. Just a simple starting point I guess.
@floor \d+\.?\d*(?:e[+-]?\d+)? is a good regex to match all valid numeric constants
I updated the regex to avoid repeating the definition of the number, and to include and expand upon @PatrickRoberts notation so that it allows capital E and prohibits a trailing . to avoid 3.e4, though that's a matter of opinion.
Not a big deal, but [eE] can be e if you specify the case-insensitive flag at the end of the regex literal.
3

You can use math.js for this.

For your case use math.evaluate(expression)

If you want to check whether an expression(containing variables) is mathematically valid use math.parse(expression) E.g. math.parse('a+b') is valid but math.parse('b+') will throw an exception.

function isValidMathExpression(expr){
        try{
            math.parse(expr);
            return true;
        }
        catch(ex){
            return false;
        }
    }

Comments

3

Using complex-js, you can wrap Complex.compile() in a try/catch statement:

function isMathExpression (str) {
  try {
    Complex.compile(str);
  } catch (error) {
    return false;
  }
  
  return true;
}

console.log(isMathExpression('2+2'))
console.log(isMathExpression('foo+bar'))
console.log(isMathExpression('sin(5)+sqrt(2/5i)'))
<script src="https://unpkg.com/[email protected]/dst/complex.min.js"></script>

The grammar for this parser is here, if you're interested in seeing what constructs it supports.

Full disclosure, I am the author of this library

Comments

0

You can use Regular Expressions. Go to that link to learn more about them.

This is a very simple example that just checks for a number and then a +, -, *, or / and then another number. You will want to adjust it and add to it to suit your needs.

/\d+(\+|\-|\*|\/)\d+/.test("1+2") // returns true
/\d+(\+|\-|\*|\/)\d+/.test("a+b") // returns false

Comments

0

you can use eval(expression) to execute it, and you need to use "trycatch" to wrap the code block. if it catches an error, the expression should be invalid

Comments

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.