0

I am having this entry in a JSON file of mine:

{
    "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
                }
            ]
        }
    ]
}

It is correct JSON according to online parsers.

What I mean with the specific regex under "condition": "regexCheck" is: A string consisting of 1-24 symbols that are either being a-z, or A-Z, or 0-9, or _, or ( or ).

How I apply it in 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
    };
}

However the regex does not seem to be working, and when inspecting the RegExp object I see the following: [a-zA-Z0-9_\(\)]{1-24}.

Does anyone know what is wrong with it?

Also, in PHP I use the following:

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);
}
2
  • What is the value of value when applying the regex? Commented Dec 6, 2013 at 20:06
  • @Danny A normal string, for example "skiwi2". Commented Dec 6, 2013 at 20:09

1 Answer 1

4

You can't use a hyphen in a quantifier. You have {1-24}

Change it to {1,24}

Also note, the regex coming back when inspecting the RegExp is correct because \ is a reserved escape character in JSON.

Sign up to request clarification or add additional context in comments.

1 Comment

I should note that the JavaScript part only works correctly as expected when I use: pass = new RegExp(argument).test(value);.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.