javascript - Regex containg a variable -


this question has answer here:

regex , variables seem current issue here, yet didn't find answer problem.

it quite simple, using nosql database system javascript called nedb. pretty neat way. github link

here part matters :

// finding planets name contain substring 'ar' using regular expression db.find({ planet: /ar/ }, function (err, docs) {   // docs contains mars , earth }); 

as see, expression /ar/ means "containing substring ar", classic regex. want replacing ar variable (the result of user search).

like :  var search = ‘ar'; //taken html form db.find ({planet : ‘/‘ + search + ‘/‘}, fonction (err,docs) { } 

this unfortunately not work. neither :

  var search = ‘/ar/';      db.find ({planet : search}, fonction (err,docs) {     } 

do have ideas ? might seem prettu obvious you, losing mind on issue aha ! thank guys

use regexp constructor :

db.find({ planet: new regexp(yourstring) }, function (err, docs) { 

if string can anything, you'd better escape it. see this related answer then.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -