Start a new topic

Regular Expressions with new rules

I read on another post, that you changed policies on regular expressions and that they must have an anchor at the beginning. Is it right?

Since that one was in "REST API" section of the forum, I opened a new one on Business Logic that fits more my question.



I'm trying to fix my queries, but I can't find a solution... earlier on I used something like this:



var query = {'name': { $regex: searchString, $options: 'i' }};

modules..collectionAccess.collection('Collection').find(query, function (err, docs) { } });



Doesn't matter about the case-insensitive option, I'll find a way around, like the one suggested in the other post, but at least how should I rewrite the query to find every document where the 'name' field begins with searchString?



I tried solutions like these but they're not working...



var query = {'name': { $regex: '^searchString' }};

var query = {'name': { $regex: /^searchString/ }};



Thanks :)

Your first suggested solution should work. The following query is currently working fine for me from business logic:



modules.collectionAccess.collection("newCollection").find( { 'dayOfWeek' : { $regex : '^T' } }, function(err, result) {

if (err) {

response.body = err;

}

response.body = result;

response.complete();

});
Uhhh... I understand now. The string associated to Regex now is an actual string that contains escape characters... while my searchString is a variable itself.



So I just needed to create the string merging searchString with escape characters.



I tried now with



var query = {'name': { $regex: '^' + searchString }};



and works :)



Thanks Gal :) your '^T' gave the right hint, cause I thought "T is a weird name for a variable...uhh it's an actual string!" :D
Login or Signup to post a comment