title: MongoDB Node JS escaping single quote problems
body: <p>In my Node js, I have escaped single quote with the below function</p>
  
  <pre><code>var regescape = function(text) {
  return text.replace(/[\[\]']+/g, "\\$&amp;"); 
  };
  </code></pre>
  
  <p>This is working fine for me.
  But suddenly I discovered I have a string M'$ in my database. Which is not returning with my below query.</p>
  
  <pre><code>param 1 = "M'$";
  
  var cursor = db.collection('search').find({"searchcontent.name":new RegExp('^'+regescape(param1))}).limit(10);
  </code></pre>
  
  <p>Also Please suggest the best practice for handling Node JS parameter pass to MongoDB. 
  I am calling NodeJS from PHP code. And I am sending parameters with <code>rawurlencode()</code> from PHP code. In node js I'm using <code>decodeURI()</code> to the received parameters.</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>My PHP code for calling Node JS:</p>
  
  <pre><code>function getdetail($data1) {
      $p1 = $data1;
      $service_url = 'http://exampleserver:8081/search?param1='.$p1;
      $curl = curl_init($service_url);
      curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
      $curl_response = curl_exec($curl);
          if ($curl_response === false) {
          $info = curl_getinfo($curl);
          curl_close($curl);
          die('error occured.Please try later');
          }
      curl_close($curl);
      $decoded = json_decode($curl_response, true);
      if (isset($decoded-&gt;response-&gt;status) &amp;&amp; $decoded-&gt;response-&gt;status == 'ERROR') {
          die('error occured.Please try later');
      }
      return $decoded;
  
  }
  </code></pre>
  
  <p>Node JS code to receive data:</p>
  
  <pre><code>app.get('/search', function (req, res) {
      var param1=decodeURI(req.query.param1);
      MongoClient.connect(url, function(err, db) {
       assert.equal(null, err);
       search(param1,db, function(data){ db.close(); res.end(JSON.stringify(data)); });
     });
  });
  
  
  var search = function(param1,db, callback) {
  
  var cursor = db.collection('search').find({$or:[{"searchcontent.name":new RegExp('^'+regescape(param1))},{"searchcontent.name":new RegExp('^'+regescape(param1.substring(0,4)))}]}).limit(10);
  
      cursor.toArray(function(err, items) {
      callback(items);
      });
  
    };
  </code></pre>
  
  <p>This Node JS code is for search so I used <code>RegEXP()</code>. For other cases I used the below code:</p>
  
  <pre><code>var cursor = db.collection('employees').find({"dep.name":regescape(param1),"mrg.name":regescape(param2)});
  </code></pre>