Search code examples
node.jsmongodbmongoose

mongoose query Regex with special characters


I want to search values having special characters such as "(" in a document. I 'm using following criteria in mongoose and fetch the names that matches like "abc (pvt) ltd".

 var criteria = {};
 criteria.name = new RegExp(searchPrameters.name, "i");

Solution

  • I found the solution with replacing of the parenthesis using string.replace() function.

     searchPrameters.name = searchPrameters.name.replace('(','\\(');
     searchPrameters.name = searchPrameters.name.replace(')','\\)');