Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在JSON中指定正则表达式字符串,在JavaScript和PHP中使用它_Javascript_Php_Regex_Json - Fatal编程技术网

在JSON中指定正则表达式字符串,在JavaScript和PHP中使用它

在JSON中指定正则表达式字符串,在JavaScript和PHP中使用它,javascript,php,regex,json,Javascript,Php,Regex,Json,我在我的JSON文件中有这个条目: { "rules": [ { "field": "ingame", "rules": [ { "condition": "minLength", "argument": 1, "php": false },

我在我的
JSON
文件中有这个条目:

{
    "rules": [
        {
            "field": "ingame",
            "rules": [
                {
                    "condition": "minLength",
                    "argument": 1,
                    "php": false
                },
                {
                    "condition": "maxLength",
                    "argument": 24,
                    "php": false
                },
                {
                    "condition": "zeroRows",
                    "argument": "SELECT * FROM users WHERE ingame = ?",
                    "php": true
                },
                {
                    "condition": "regexCheck",
                    "argument": "[a-zA-Z0-9_\\(\\)]{1-24}",
                    "php": false
                }
            ]
        }
    ]
}
根据在线解析器,它是正确的
JSON

我对
条件下的特定
regex
的意思是:“regexCheck”
是:由1-24个符号组成的字符串,这些符号要么是
A-z
,要么是
A-z
,要么是
0-9
,要么是
,要么是

如何在JavaScript中应用它:

function checkRule(condition, argument, value) {
    var pass = false;
    var errormessage = "";
    switch (condition) {
        case "regexCheck":
            pass = value.match(new RegExp(argument));
            if (!pass) {
                errormessage = "Not in expected format";
            }
            break;
    }
    return {
        pass: pass,
        errormessage: errormessage
    };
}
然而,正则表达式似乎不起作用,当检查
RegExp
对象时,我看到以下内容:
[a-zA-Z0-9\(\)]{1-24}

有人知道它有什么问题吗?

此外,在PHP中,我使用以下内容:

function checkRules($rules, $name, $value) {
    $pass = false;
    $errormessage = "";
    if (file_exists("../rules/{$rules}.json")) {
        $rules = json_decode(file_get_contents("../rules/{$rules}.json", true));
        foreach ($rules->rules as $fvalue) {
            if ($fvalue->field == $name) {
                foreach ($fvalue->rules as $ruleset) {
                    switch ($ruleset->condition) {
                        case "regexCheck":
                            $pass = preg_match($ruleset->argument, $value);
                            if (!$pass) {
                                $errormessage = "Not in expected format";
                            }
                            break;
                    }
                    if (!$pass) {
                        break;
                    }
                }
                break;
            }
            else {
                $pass = true;
                $errormessage = "";
            }
        }
    }
    else {
        $pass = false;
        $errormessage = "Internal error";
    }
    return array($pass, $errormessage);
}

不能在量词中使用连字符。您有
{1-24}

将其更改为
{1,24}


还要注意,检查RegExp时返回的正则表达式是正确的,因为
\
是JSON中的保留转义字符。

您不能在量词中使用连字符。您有
{1-24}

将其更改为
{1,24}


另外请注意,检查RegExp时返回的正则表达式是正确的,因为
\
是JSON中的保留转义字符。

当将正则表达式应用于普通字符串(例如
“skiwi2”
)时,
值的值是多少,例如
“skiwi2”
。我应该注意,JavaScript部分只有在使用时才能正常工作:
pass=new RegExp(参数).test(值)。我应该注意,JavaScript部分只有在使用时才能正常工作:
pass=newregexp(参数)。test(值)