5

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>
2
  • 4
    I did proofread it, at least 4 times. It's a case of someone else being so much better at spotting that silly mistake you make, you may have come across it yourself. Commented Sep 23, 2008 at 12:46
  • 1
    @Geoffrey: Actually, it's JavaScript. Commented Oct 27, 2010 at 8:51

6 Answers 6

17

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>

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

Comments

8

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.

2 Comments

You've missed the question itself
Yeah, I've seen that now in Paul's answer. It was a simple case of me being extra stupid. Thanks for the help anyway :)
4
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');

Comments

2

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.

Comments

1

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

1 Comment

As with rslite, you've missed the actual question, I want to find a way to run such a regex and catch the resultant error
0

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

Comments

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.