JSFiddle - React, Tailwind, and code Playground
HTML
<form onsubmit="return false;"><input type="text" id="expression" /> <button id="evaluate">Evaluate</button></form>
JavaScript
document.getElementById("evaluate").onclick = function () {
mathEval(document.getElementById("expression").value);
}
function mathEval (exp) {
var reg = /(?:[a-z$_][a-z0-9$_]*)|(?:[;={}\[\]"'!&<>^\\?:])/ig,
valid = true;
// Detect valid JS identifier names and replace them
exp = exp.replace(reg, function ($0) {
// If the name is a direct member of Math, allow
if (Math.hasOwnProperty($0))
return "Math."+$0;
// Otherwise the expression is invalid
else
valid = false;
});
// Don't eval if our replace function flagged as invalid
if (!valid)
alert("Invalid arithmetic expression");
else
try { alert(eval(exp)); } catch (e) { alert("Invalid arithmetic expression"); };
}