Alex Rivera | Logout

Creating regexp with special characters

Asked 2012-01-16T16:00:19.767
9

I'm creating a query for mongodb:

app.get('content/:title',
function(req, res) {
  var regexp = new RegExp(req.params.title, 'i');

  db.find({
    "title": regexp,
  }).toArray(function(err, array) {
    res.send(array);
  });
});

But sometimes the title has a parenthese in it. This gives me the error:

SyntaxError: Invalid regular expression: /cat(22/: Unterminated group
    at new RegExp (unknown source)

The title that is being searched for is cat(22).

What's the easiest way to make the regex accept parenthesis? Thanks.

Edit
Report

1 Answer

1

Escape it with a backslash. And test it on a site like http://rejex.heroku.com/

/cat\(22/
answered 2012-01-16T16:07:47.057

Your Answer