我正在为计算器项目执行此操作,我想检查操作是否有效,如果`$echo "Welcome to the server"
$echo "Hi this is content in the test file1."
$ls
$date`
,我无法检查eval
的布尔值?在控制台上:
false
与Boolean(eval('2+2(9.1-)9'));
Boolean(2+2(9.1-)9); // Both operations return unexpected token
< - 不同,返回Boolean(2+2)
。帮助
答案 0 :(得分:1)
您实际上无需评估代码以查看代码是否有效 - 仅$index = 13;
$value = current(array_filter($array,
function($key) use ($index) {
if(!is_int(strpos('-', $key))){
return $key == $index;
}
$range = explode('-', $key);
return ($index >= $range[0] && $index <= $range[1]);
}, ARRAY_FILTER_USE_KEY));
创建try
:
Function
function checkIt() {
var fn
try {
var fn = new Function(document.getElementById("code").value)
alert("Great, that's a valid piece of code!")
} catch (e) {
alert("That's not a valid piece of code.")
}
}
例如,尝试“123”,“有效”和“''不[有效!!!”。
虽然 将立即对其进行评估,但如果它有效,您应该只检查错误是否是语法错误,或者其他。
<input id="code">
<button onclick="checkIt()">Check it</button>
function doIt() {
var fn
try {
var result = eval(document.getElementById("code").value)
alert("The result is: " + result)
} catch (e) {
if (e instanceof SyntaxError) {
alert("That's not a valid piece of code.")
} else {
alert(e.message)
}
}
}
例如,尝试您之前尝试过的相同内容,看看它的行为方式。
答案 1 :(得分:0)
如果我理解你的问题,你唯一想知道的是&#34;这是一个有效的表达&#34;。检查此问题的一种非常简单的方法是使用eval()
,就像在此处一样,并将其括在try
中,并查看是否发生任何错误。例如,你可以这样写:
try {
eval('2+2(9.1-)9');
valid = true;
} catch (e) {
valid = false;
}
然后,如果表达式有效,变量valid
包含true
,如果不是,则false
包含eval()
。
警告angular.module('App')
.factory('friendsFactory', function(){
var friends = [];
var friendshipRef = firebase.database().ref('friendships');
friendshipRef.on('value', function(snapshot){
//etc.. updates friends array
});
return{
getFriends: function(){
return friends;
},
//etc
}
:每个有效代码都会通过此测试,而不仅仅是数学表达式:此外,该代码将被执行。小心你给它的字符串。