首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

Js容易判断四则运算是否可用

2014-01-15 
Js简单判断四则运算是否可用function?test(s){?????try{????????return?!isNaN(eval((+s+)))????}cat

Js简单判断四则运算是否可用
function?test(s){?????try{????????return?!isNaN(eval("("+s+")"));????}catch(e){????????return?false;????}}alert(test("1+6*(2+(3-1)/1+(9-5*1))"));//--------------------------------------------------------------------------------------<script>var?str?=?"1+6*(2+(3-1)/1+(9-5*1))";try{????eval("var?ret?=?"+str);????if(ret!=Infinity){????????alert("right");????}else{????????alert("wrong");????}}catch(e){????alert("wrong");}</script>//--------------------------------------------------------------------------------------function?test(s){????var?reg=/[\d\.\+\-\*\/\(\)]+/;????try{????????return?ret.test(s)&&!isNaN(eval("("+s+")"));????}catch(e){????????return?false;????}}alert(test("1+6*(2+(3-1)/1+(9-5*1))"));//--------------------------------------------------------------------------------------<script>var?s?=?'1+6*(2+(3-1)/1+(9-5*1))';if(s.match(/[^\+\-\*\/0-9()]/)){????alert('illegal!')}else{????try????{????????alert(eval(s));????}????catch(e)????{????????alert('illegal')????}}</script>

热点排行