Testing for the existence of a substring

Folks,

I belive I have solved this repeated times, but it is an issue with the documentation that I cant find the answer again. Consider I have a string of text such as {{My heading||inner-link|!!!}} maybe something, in fact it is a line in a tiddler text field.

So now I am writting a TiddlyWiki script that will use each line in which I can find ||inner-link| then I will do something with the whole line. So All I want to do is to test if the string {{My heading||inner-link|!!!}} maybe something to see if it contains the substring ||inner-link| and leave me with the full string. Similar to testing for prefix or suffix.

Of course you will quickly see the contains operator is more a “list contains filter” ie; filter the input by searching list fields for a value.

The search operator operates accross tiddlers and fields.

I know I may be able to use regular expressions but I want the oportunity to provide the substring via a literal, variable or transclusion (reference). I think this is a missing operator? contains-string[]?

  • Its fine if it is also the prefix, suffix, or only string.

I have created a contains-string[] operator based on the match[] operator and it works (tested)

  • Care of ChatGPT

You might want to take a look at: How to Extract Delimited Substrings from a Longer String - Tips & Tricks - Talk TW

1 Like

Mmm, something like this : [<list of strings>search:title<searchstring>] ?

More like this;

\define string() {{My heading||inner-link|!!!}} 
\define substring() ||inner-link|

{{{ [<string>contains-string<substring>] }}}

modules_filters_contains-string.js.json (1.7 KB)

warning this is my own script kiddy code.

\define substring() ||inner-link|

{{{ [all[current]get[text]splitregexp[\n]contains-string<substring>] }}}

list each line containing the substring.

The substring has to be escaped first. Otherwise the regexp interpreter may not understand it in the right way.

See: https://tiddlywiki.com/#escaperegexp%20Operator

\define substring() ||inner-link|
\define input()
line with ||inner-link| and some text
||inner-link|at the start
some text and ||inner-link|
some text
line of text and ||inner-link | with a space
an other line of text with a bit of ||inner-link
\end

\function f.substring() [<substring>escaperegexp[]] 

\function f.contains-string() [<currentTiddler>] :filter[<currentTiddler>regexp<f.substring>]

{{{ [<input>splitregexp[\n]] :filter[<currentTiddler>regexp<f.substring>] }}}

----

{{{ [<input>splitregexp[\n]] :map[f.contains-string[]] }}}

----

{{{ [<input>splitregexp[\n]] :map[f.contains-string<substring>] }}}

2 Likes