diff --git a/lib/query/index.js b/lib/query/index.js index 51086471d..4c0bff630 100644 --- a/lib/query/index.js +++ b/lib/query/index.js @@ -213,7 +213,7 @@ Query.prototype.parseExpression = function parseExpression(field, expression) { // Handle `contains` by building up a case insensitive regex if(modifier === 'contains') { val = utils.caseInsensitive(val); - val = '.*' + val + '.*'; + val = '[\\s\\S]*' + val + '[\\s\\S]*'; obj['$regex'] = new RegExp('^' + val + '$', 'i'); return obj; } @@ -221,7 +221,7 @@ Query.prototype.parseExpression = function parseExpression(field, expression) { // Handle `like` if(modifier === 'like') { val = utils.caseInsensitive(val); - val = val.replace(/%/g, '.*'); + val = val.replace(/%/g, '[\\s\\S]*'); obj['$regex'] = new RegExp('^' + val + '$', 'i'); return obj; } @@ -229,7 +229,7 @@ Query.prototype.parseExpression = function parseExpression(field, expression) { // Handle `startsWith` by setting a case-insensitive regex if(modifier === 'startsWith') { val = utils.caseInsensitive(val); - val = val + '.*'; + val = val + '[\\s\\S]*'; obj['$regex'] = new RegExp('^' + val + '$', 'i'); return obj; } @@ -237,7 +237,7 @@ Query.prototype.parseExpression = function parseExpression(field, expression) { // Handle `endsWith` by setting a case-insensitive regex if(modifier === 'endsWith') { val = utils.caseInsensitive(val); - val = '.*' + val; + val = '[\\s\\S]*' + val; obj['$regex'] = new RegExp('^' + val + '$', 'i'); return obj; } @@ -366,12 +366,15 @@ Query.prototype.parseValue = function parseValue(field, modifier, val) { return val; } - if(!validator.isMongoId(val)){//only if it's not mongodbID, for most of case usage would like: user.find('56173df732776c64852f8c91') + // Only if it's not mongodbID, for most of case usage would like: + // user.find('56173df732776c64852f8c91') + if(!validator.isMongoId(val)){ // Replace Percent Signs, work in a case insensitive fashion by default val = utils.caseInsensitive(val); - val = val.replace(/%/g, '.*'); + val = val.replace(/%/g, '[\\s\\S]*'); val = new RegExp('^' + val + '$', 'i'); } + return val; }