From 8ef520ef37801986a03757f4f53c17e0ccf4c263 Mon Sep 17 00:00:00 2001 From: Stephan Hradek Date: Fri, 10 Jan 2014 10:32:49 +0100 Subject: [PATCH] Created regexp search for milestone 5.1 --- core/modules/filters.js | 26 ++++++++++++++++++++------ core/modules/filters/field.js | 8 +++++++- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/core/modules/filters.js b/core/modules/filters.js index 592cb7b97..386a60df6 100644 --- a/core/modules/filters.js +++ b/core/modules/filters.js @@ -52,12 +52,25 @@ function parseFilterOperation(operators,filterString,p) { operator.operator = "title"; } // Get the operand - bracketPos = filterString.indexOf(operator.indirect ? "}" : "]",p); - if(bracketPos === -1) { - throw "Missing closing bracket in filter expression"; + if(!operator.indirect && filterString.charAt(p) === "/") { + // regexp + var rex = /^\/((?:[^\\\/]*|\\.))*\/([igm]*)\]/g; + var rexMatch = rex.exec(filterString.substring(p)); + if(!rexMatch) { + throw "Incomplete regular expression in filter expression"; + } + operator.regexp = new RegExp(rexMatch[1], rexMatch[2]); + operator.operand = filterString.substr(p,rex.lastIndex-1); + p += rex.lastIndex; + } + else { + bracketPos = filterString.indexOf(operator.indirect ? "}" : "]",p); + if(bracketPos === -1) { + throw "Missing closing bracket in filter expression"; + } + operator.operand = filterString.substring(p,bracketPos); + p = bracketPos + 1; } - operator.operand = filterString.substring(p,bracketPos); - p = bracketPos + 1; // Push this operator operators.push(operator); } while(filterString.charAt(p) !== "]"); @@ -161,7 +174,8 @@ exports.compileFilter = function(filterString) { results = operatorFunction(accumulator,{ operator: operator.operator, operand: operand, - prefix: operator.prefix + prefix: operator.prefix, + regexp: operator.regexp },{ wiki: self, currTiddlerTitle: currTiddlerTitle diff --git a/core/modules/filters/field.js b/core/modules/filters/field.js index 8fff9114b..7657f8441 100644 --- a/core/modules/filters/field.js +++ b/core/modules/filters/field.js @@ -21,7 +21,13 @@ exports.field = function(source,operator,options) { function checkTiddler(title) { var tiddler = options.wiki.getTiddler(title); if(tiddler) { - var match = tiddler.getFieldString(operator.operator) === operator.operand; + var match; + if(operator.regexp) { + match = !! operator.regexp.exec(tiddler.getFieldString(operator.operator)); + } + else { + match = tiddler.getFieldString(operator.operator) === operator.operand; + } if(operator.prefix === "!") { match = !match; }