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

标签 javascript php regex json

我的 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

我对 "condition": "regexCheck" 下的特定 regex 的意思是:由 1-24 个符号组成的字符串,这些符号可以是 a-z A-Z0-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 中的保留转义字符。

关于javascript - 在 JSON 中指定正则表达式字符串,在 JavaScript 和 PHP 中使用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20432529/

相关文章:

regex - 如何使用 golang 进行字符串替换

javascript - 从字符串中删除值

parent 的javascript parent

javascript - 如何使用 bower 在 javascript 项目中包含私有(private)本地文件

PHP 保留字作为命名空间和类名

PHP preg_replace 反向引用导致未定义常量通知

javascript - 通过 ASP.net 在 javascript 中发出警报日期

php - 如何通过行号从 CSV 文件中删除特定行?

php - 使用 RegEx 提取表单字段

regex - 如何使正则表达式模式的某些部分有条件?