I'm pretty sure there isn't an easier way to do what you need without first escaping "magic" RegExp characters using a function like:
function escapeRegExpChars(text) {
return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
}
So, assuming you are searching for the user handle (pseudo) "$Foo(.)Bar+", you would:
var query = new RegExp(escapeRegExpChars(handle),'i')
which produces:
/\$Foo\(\.\)Bar\+/i
But there are drawbacks to using a RegExp to search for records in a database, the most onerous being that the RegExp must be applied to each record individually. This is called a "full scan" of the table and is very inefficient.
To improve performance considerably (by removing the requirement for a full table scan), I'd keep two copies of the handle in your database, one for display purposes and the other to ease search.
To create the search oriented handle, you could:
function createEscapedHandle(handle){
return handle.toLowerCase().replace(/(\W)/g,function(ch){
return '%' + ch.charCodeAt(0).toString(16);
});
}
var handle = '$Foo(.)Bar+',
escapedHandle = createEscapedHandle(handle);
// where escaped handle is '%24foo%28%2e%29bar%2b'
Save both the original handle and its escaped version in your db, and search on the escaped version.
I believe you'll find that this trade-off between processing (matching the RegExp against every record) vs. the extra storage space required to save the escaped version of the handle will provide you significantly faster results.