This page is part of a static HTML representation of the TiddlyWiki at https://tiddlywiki.com/

search-replace Operator (Examples)

 1st November 2021 at 12:52pm

Replace one string with another:

[[The quick brown fox jumps over the lazy dog]search-replace[dog],[cat]]

Replace all matches of a regular expression:

\define myregexp() e|o

[[Hello There]search-replace:g:regexp<myregexp>,[]]

Replace all matches of a string irrespective of case:

[[Hello There]search-replace:gi[H],[]]

You can also use regular expression capture groups in the replacement string: \define names() (\w+)\s(\w+)

[[John Smith]search-replace::regexp<names>,[$2,$1]]

You can reference the portion of the input that matches the regular expression with $&:

[[John Smith]search-replace::regexp[John .*],[His name is $&]]

[[This is an exciting feature]search-replace::regexp[exciting],[amazing and $&]]

To replace everything but a match using a regular expression and the multiline (m) flag: \define myregexp2() ^(?!Unlike).*$

[[HelloThere]get[text]search-replace:gm:regexp<myregexp2>,[]]

You can use the search-replace Operator in combination with the sortsub Operator to ignore stop words like "A" and "The" at the beginning of titles when sorting. (Note for example that "A Tale of Two Cities" sorts under "T" rather than "A")

[enlist<book-list>sortsub<compare-without-stopwords>]

You can also customise the listing so that "A Tale of Two Cities" is listed as "Tale of Two Cities, A" but still links to the correct tiddler.

<ul>
<$list filter="[enlist<book-list>sortsub<compare-without-stopwords>]">
<li><$link><$text text={{{[<currentTiddler>search-replace:i:regexp[(^The |A )(.*)],[$2, $1]]}}}/></$link></li>
</$list>
</ul>

That renders as: