Question

I've got a function that runs a user generated Regex. However, if the user enters a regex that won't run then it stops and falls over. I've tried wrapping the line in a Try/Catch block but alas nothing happens.

If it helps, I'm running jQuery but the code below does not have it as I'm guessing that it's a little more fundamental than that.

Edit: Yes, I know that I am not escaping the "[", that's intentional and the point of the question. I'm accepting user input and I want to find a way to catch this sort of problem without the application falling flat on it's face.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
    <title>Regex</title>

    <script type="text/javascript" charset="utf-8">
        var grep = new RegExp('gr[');

        try
        {
            var results = grep.exec('bob went to town');
        }
        catch (e)
        {
            //Do nothing?
        }

        alert('If you can see this then the script kept going');
    </script>
</head>
<body>

</body>
</html>
Was it helpful?

Solution

Try this the new RegExp is throwing the exception

Regex

    <script type="text/javascript" charset="utf-8">
            var grep;

            try {
                    grep = new RegExp("gr[");
            }
            catch(e) {
                    alert(e);

            }
            try
            {
                    var results = grep.exec('bob went to town');
            }
            catch (e)
            {
                    //Do nothing?
            }

            alert('If you can see this then the script kept going');
    </script>

OTHER TIPS

The problem is with this line:

var grep = new RegExp('gr[');

'[' is a special character so it needs to be escaped. Also this line is not wrapped in try...catch, so you still get the error.

Edit: You could also add an

alert(e.message);

in the catch clause to see the error message. It's useful for all kind of errors in javascript.

Edit 2: OK, I needed to read more carefully the question, but the answer is still there. In the example code the offending line is not wrapped in the try...catch block. I put it there and didn't get errors in Opera 9.5, FF3 and IE7.

var grep, results;

try {
    grep = new RegExp("gr[");
    results = grep.exec('bob went to town');
}
catch(e) {
    alert(e);
}
alert('If you can see this then the script kept going');

putting the RegExp initialization inside the try/catch will work (just tested in FireFox)


var grep, results;

try
{
    grep = new RegExp("gr["); // your user input here
}
catch(e)
{
    alert("The RegExpr is invalid");
}

// do your stuff with grep and results

Escaping here is not the solution. Since the purpose of this snippet is to actually test a user-generated RegExpr, you will want to catch [ as an unclosed RegExpr container.

your RegExp doesn't close the [

In my FireFox, it never returns from the constructor -- looks like a bug in the implementation of RegExp, but if you provide a valid expression, it works

One option is to validate the user-generated expressions. That is; escape characters that you know will stall your script.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top