1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-30 05:19:57 +00:00

Make sure split(regex) returns an array of strings (#8222)

* make sure split(regex) returns an array of strings

* remove "undefined" from the output

* add info about capture groups to the docs
This commit is contained in:
Mario Pietsch 2024-06-08 18:09:21 +02:00 committed by GitHub
parent a67c0e1399
commit 12c551ef05
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 26 additions and 5 deletions

View File

@ -127,7 +127,7 @@ function diffPartsToChars(text1,text2,mode) {
if(lineHash.hasOwnProperty ? lineHash.hasOwnProperty(line) : (lineHash[line] !== undefined)) {
chars += String.fromCharCode(lineHash[line]);
} else {
if (lineArrayLength == maxLines) {
if(lineArrayLength == maxLines) {
line = text.substring(lineStart);
lineEnd = text.length;
}
@ -217,7 +217,10 @@ exports.splitregexp = function(source,operator,options) {
return ["RegExp error: " + ex];
}
source(function(tiddler,title) {
Array.prototype.push.apply(result,title.split(regExp));
var parts = title.split(regExp).map(function(part){
return part || ""; // make sure it's a string
});
Array.prototype.push.apply(result,parts);
});
return result;
};
@ -264,7 +267,7 @@ exports.pad = function(source,operator,options) {
} else {
var padString = "",
padStringLength = targetLength - title.length;
while (padStringLength > padString.length) {
while(padStringLength > padString.length) {
padString += fill;
}
//make sure we do not exceed the specified length

View File

@ -1,6 +1,6 @@
caption: splitregexp
created: 20190613154722705
modified: 20190613154924724
modified: 20240606113433618
op-input: a [[selection of titles|Title Selection]]
op-output: the input titles split into separate items according to the specified regular expression <<.place R>>
op-parameter: The regular expression at which to split each title
@ -13,7 +13,7 @@ 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,
<<.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]]
@ -42,3 +42,21 @@ Syntax errors in the regular expression will cause the filter to return an error
<<.operator-example 2 "[[the cat sat on the mat]splitregexp[\]]">>
<<.operator-examples "splitregexp">>
----
The <<.op splitregexp>> operator is intended to be used as described above. If the `regexp` contains //capture groups// those groups will be included into the output.
<<.bad-example """```
\procedure re() (color)|(colour)ed
\procedure str() Some coloured text
{{{ [<str>splitregexp<re>join[, ]] }}}
```""">>
Somewhat more useful may be this code.
```
\procedure re() (colou?red)
\procedure str() Some coloured text
{{{ [<str>splitregexp<re>join[, ]] }}}
```