Validation of mathematical formulas with regular expressions (with variables, JS version)

Source: Internet
Author: User

I had a friend today who asked me to write a regular expression that would verify that a mathematical formula is well-formed.

There are parentheses, variables (unknowns) and operators in the mathematical formula, and the variables are read in the database and can be arbitrarily added and deleted.

This implementation with a regular expression is not possible, so I wrote him a function, as follows:

(function () {/* * if variable selected: ID,NUM,TOTAL,AVL TEST * Correct formula example: id*num+ (TOTAL/AVL) *0.5 * Wrong formula example: id**|0.5 */function fn (String, OBJ) {//TODO: How to handle =? Remove whitespace character string = String.Replace (/\s/g, ');//Error condition, empty string if ("" = = = String) {return false;} Error condition, operator continuous if (/[\+\-\*\/]{2,}/.test (string)) {return false;} Empty parenthesis if (/\ (\)/.test (string)) {return false;} Error conditions, brackets do not match to var stack = [];for (var i = 0, item; i < String.Length; i++) {item = String.charat (i); if (' (' = = = Item) {Stack . Push (' (');} else if (') ' = = = Item} {if (Stack.length > 0) {stack.pop ();} Else{return false;}}}   if (0!== stack.length) {return false;} Error condition, (followed by operator if (/\ ([\+\-\*\/]/.test (String)) {return false;} Error condition,) preceded by operator if (/[\+\-\*\/]\)/.test (String)) {return false;} Error condition (not preceded by operator if (/[^\+\-\*\/]\ (/.test (String)) {return false;} Error condition,) is not followed by operator if (/\) [^\+\-\*\/]/.test (String)) {return false;} The error condition, the variable does not come from "to choose Formula variable" var tmpstr = string.replace (/[\ (\) \+\-\*\/]{1,}/g, ""); var array = Tmpstr.split ("); for (var i = 0 , item; i < Array.Length; i++) {Item= Array[i];if (/[a-z]/i.test (item) && ' undefined ' = = = = typeof (Obj[item])) {return false;}}        return true;}    Test var fields = {' IDs ': 1, ' total ': 1, ' AVL ': 1, ' NUM ': 1}; The string submitted to the server should not contain whitespace characters, or the user should be prevented from entering the white space character alert (FN (' id*num+ (TOTAL/AVL) *0.5 ', fields));}) ();


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.