index.htmlにアクセスがあったら、JavaScriptでURLから演算式を取り出して計算結果を表示するようにしたいです。
今、以下のような状態なのですがアドバイスをお願い致します。
html
1<!DOCTYPE html> 2<html> 3<head></head> 4<body> 5<script type="text/javascript"> 6//演算式を取得 7var equation =location.search.substring(1); 8//演算式になっていれば計算する 9if( equation.match(/+|-|\*|\/|(|)/){ 10document.write( equation ); 11} else { 12document.write("ERROR"); 13} 14</script> 15</body> 16</html>
以下のようにアクセスがあります。
curl 'http://example.co.jp/calc?abc'
ERROR
curl 'http://example.co.jp/calc?1+1'
2
curl 'http://example.co.jp/calc?1+2*3'
7
curl 'http://example.co.jp/calc?(1+2)*3'
9
回答1件
あなたの回答
tips
プレビュー