diff --git a/core/modules/filters/strings.js b/core/modules/filters/strings.js index c62dcaac6..27273ca41 100644 --- a/core/modules/filters/strings.js +++ b/core/modules/filters/strings.js @@ -39,13 +39,17 @@ exports.trim = makeStringBinaryOperator( ); exports.split = makeStringBinaryOperator( - function(a,b) {return ("" + a).split(b).filter(function(str) {return !!str;});} + function(a,b) {return ("" + a).split(b);} ); exports.join = makeStringReducingOperator( function(accumulator,value,operand) { - return "" + (accumulator ? accumulator + (operand || "") + value : value); - } + if(accumulator === null) { + return value; + } else { + return accumulator + operand + value; + } + },null ); function makeStringBinaryOperator(fnCalc) { @@ -59,7 +63,6 @@ function makeStringBinaryOperator(fnCalc) { } function makeStringReducingOperator(fnCalc,initialValue) { - initialValue = initialValue || ""; return function(source,operator,options) { var result = []; source(function(tiddler,title) { @@ -75,10 +78,15 @@ exports.splitregexp = function(source,operator,options) { var result = [], suffix = operator.suffix || "", flags = (suffix.indexOf("m") !== -1 ? "m" : "") + (suffix.indexOf("i") !== -1 ? "i" : ""), - regExp = new RegExp(operator.operand || "",flags); + regExp; + try { + regExp = new RegExp(operator.operand || "",flags); + } catch(ex) { + return ["RegExp error: " + ex]; + } source(function(tiddler,title) { Array.prototype.push.apply(result,title.split(regExp)); - }); + }); return result; }; diff --git a/editions/tw5.com/tiddlers/filters/split Operator.tid b/editions/tw5.com/tiddlers/filters/split Operator.tid index 27b946772..552e7daf5 100644 --- a/editions/tw5.com/tiddlers/filters/split Operator.tid +++ b/editions/tw5.com/tiddlers/filters/split Operator.tid @@ -12,4 +12,28 @@ type: text/vnd.tiddlywiki <<.from-version "5.1.20">> +Note that in some circumstances the <<.op split>> operator will include blank items in the list of results. For example, + +``` +[[the band thethe are the best the]split[the]] +``` + +The following results are returned: + +``` +["", " band ", "", " are ", " best ", ""] +``` + +Where it might be expected that the results would be: + +``` +[" band ", " are ", " best "] +``` + +The blank items mark the boundaries between matches. If they are not required they can be removed with the ''blank'' category of the [[is Operator]]: `[[the band thethe are the best the]split[the]!is[blank]]`. + +The reason that the blank items can be useful is that they allow search and replace operations to be constructed from a combination of the [[split Operator]] or [[splitregexp Operator]] and the [[join Operator]]. For example: + +<<.operator-example 1 "[[the band thethe are the best the]split[the]join[every]]">> + <<.operator-examples "split">> diff --git a/editions/tw5.com/tiddlers/filters/splitregexp Operator.tid b/editions/tw5.com/tiddlers/filters/splitregexp Operator.tid index 085cbd1ff..59043e7f8 100644 --- a/editions/tw5.com/tiddlers/filters/splitregexp Operator.tid +++ b/editions/tw5.com/tiddlers/filters/splitregexp Operator.tid @@ -13,4 +13,32 @@ type: text/vnd.tiddlywiki <<.from-version "5.1.20">> +Note that in some circumstances the <<.op splitregexp>> operator will include blank items in the list of results. For example, + +``` +[[the band thethe are the best the]splitregexp[the]] +``` + +The following results are returned: + +``` +["", " band ", "", " are ", " best ", ""] +``` + +Where it might be expected that the results would be: + +``` +[" band ", " are ", " best "] +``` + +The blank items mark the boundaries between matches. If they are not required they can be removed with the ''blank'' category of the [[is Operator]]: `[[the band thethe are the best the]splitregexp[the]!is[blank]]`. + +The reason that the blank items can be useful is that they allow search and replace operations to be constructed from a combination of the [[split Operator]] or [[splitregexp Operator]] and the [[join Operator]]. For example: + +<<.operator-example 1 "[[nobody, really; wants; to see -- all this \punctuation]splitregexp[,|;|-|\\]join[...]]">> + +Syntax errors in the regular expression will cause the filter to return an error message. For example: + +<<.operator-example 2 "[[the cat sat on the mat]splitregexp[\]]">> + <<.operator-examples "splitregexp">>