PHP (PCRE) Validation in a Javascript Function

javascript, pcre, php, regex

Solution

Quick & Dirty AJAX code to check against PHP:

Javascript:

function regexValidator(value) {
    var req = new XMLHttpRequest();
    req.onreadystatechange = function() {
        if (req.status == 200 && req.responseText.length() > 0) {
            alert(req.responseText);
        }
    };
    req.open('GET', 'http://example.com/validator.php?r=' + escape(value), true);
    req.send();
}

PHP:

<?php
if (isset($_GET['r'])) {
    if (preg_match(urldecode($_GET['r']), '') === FALSE) {
        echo "NOT VALID";
    } else {
        echo "VALID";
    }
}

Problem

Is there an existing function (or perhaps someone wrote some existing code) to validate in a JS function that the text passed is valid PCRE? I've been Googling like crazy but the closest thing I've been able to find is just "PCRE_JAVASCRIPT_COMPAT" (which is a PCRE compatibility flag) or discussions about validating input in both PHP and Javascript. The closest thing I've seen to Javascript REGEX validation is: ``` try { "".match(regex); return true; } catch (e) { return false; } ``` This does not take into account differences between JS (ECMA) regular expressions and PHP (PCRE) regular expressions - that's obviously an issue, but I've not found a clear discussion of the differences between the two. Any help please?

Original source