Regex or loops in filter notation?

48 views
Skip to first unread message

Paul Lee

unread,
Dec 12, 2019, 2:00:31 PM12/12/19
to TiddlyWiki
I wish I could put a regex in a filter, in order to select all fields whose identifiers begin with the same group of characters in tiddlers inheriting one of my templates.

The needed functionality is to loop through different fields with the same name except for a number at the end, outputting a phrase before the value of those fields only if that particular tiddler is present in the child tiddlers that will transclude the template. The intended purpose is to list people who are the subjects of photographs; each photograph tiddler has one or more "subject#" fields containing names. A portrait of John Smith has a field subject 1: John Smith, while an expedition photo of Lewis and Clark might have subject1: Meriwether Lewis and subject2: William Clark.

Three thoughts about how to do this:
  • A regex in a $list widget, if such a thing were possible: <$list filter="[all[current]has[subject[1-9]]">
  • A for loop... I'm guessing this would require a macro? Would I define the index variable using the $set widget?
  • The $fields widget allows me to approximate what I want to do due to its useful template attribute, but it is difficult to foresee all the many fields that need to be exlcuded using its exclude attribute.
I saw that there is an escaperegexp operator, but I'm guessing that this filter notation merely escapes regex strings?

I don't really understand; I'm not a developer myself, merely a user who has learned basic JavaScript syntax.

Mohammad

unread,
Dec 12, 2019, 2:17:30 PM12/12/19
to TiddlyWiki
See 


to learn how to use regexp in Tiddlywiki.

--Mohammad

Mark S.

unread,
Dec 12, 2019, 3:09:38 PM12/12/19
to TiddlyWiki
Having individual "subject" fields will require a fair amount of wikitext programming.

You might consider using the list field to list your subjects. It depends on the overall nature and comprehensiveness of your task.

If you have a photo, say "Photo-1" and then use the "new here" button available from the tiddler's drop-down, then you can create "subjects" who are tagged with "Photo-1". You can then click on the tag "Photo-1" in any of the subjects, and rearrange the subjects. For instance, you might want to arrange the subjects in left-to-right order as they appear in the photo. Then the list of the tagged tiddlers will appear in the "list" field of "Photo-1". This has certain advantages. For one, the "list" field is one of the few fields that TW will maintain for you. So, for instance, if you have to change the title a bit ("Darn, it was Donald Trump JR."), then the reference in the list can be (if you let it) be updated when you change the title.

As you say, using "subject" fields will require you to do extra tricky steps. You could use the regexp operator (there is one), but also the "prefix" operator would probably work. But it will require at least one or more extra nested loops to do it this way.

Merry Ho Ho and a bottle of Reindeer

TonyM

unread,
Dec 12, 2019, 3:17:50 PM12/12/19
to TiddlyWiki
In my only published plugin mymenus I allow multiple menu items with fields named menu-n

I Can list these with a filter

[all[current]fields[]prefix[menu-]]

I can then process as many menu- fields that exist. Perhaps this will help?

regards
Tony

Reply all
Reply to author
Forward
0 new messages