SNR: Search and Replace in Tiddler Fields

376 views
Skip to first unread message

Mohammad

unread,
Feb 28, 2019, 1:54:26 AM2/28/19
to tiddl...@googlegroups.com
Announcement: SNR2

Update: Mar 7th, 2019

Changes:
  • new interface
  • regexp flags can be selected among i, g,m but not g and m at the same time.
  • minor issues fixed


Update: Mar 6th, 2019

Changes:
  • regexp flags added (g, m, i)
  • whole words search added
  • minor issues fixed

------

Announcement: SNR2

This is a simple script, search-n-replcae (snr2) to search in tiddler contents (including all fields) for a searchValue and replace them with replaceValue.

Demo:
http://kookma.tiddlyspot.com/    (temporarily site )


It uses a JS macro and can search-n-replace whole words and also do case-Insensitive search-n-replace.

Special thanks to Jeremy Ruston for his great solution on retrieving a JS macro result in raw text form and store it in a variable.

I also highly appreciate the invaluable help of Mark S and BTC. This tool is actually a result of collaboration between all these people.

The SNR will be part of Tiddler Commander .

--Mohammad





------

Announcement: SNR1

This is a simple script, search-n-replcae (snr) to search in tiddler contents (including all fields) for a searchValue and replace them with replaceValue.

See the original post here!





https://groups.google.com/d/msg/tiddlywiki/_OnqU7IiB9g/ukSBoPvECAAJ


--Mohammad

PMario

unread,
Feb 28, 2019, 7:21:14 AM2/28/19
to tiddl...@googlegroups.com
On Thursday, February 28, 2019 at 7:54:26 AM UTC+1, Mohammad wrote:
Announcement: SNR

This is a simple script, search-n-replcae (snr) to search in tiddler contents (including all fields) for a searchValue and replace them with replaceValue.

Nice work!

----------- OT ---------------------

It seems the tiddler folding mechanism has been broken with this tiddler.

If you want to make $:/tags/Macro code visible just add the following line at the end of the source code.

<pre><$view field="text"/></pre>

It won't interfere with the code. BUT be aware, that doesn't work with tiddlers used as templates!!!!!!

have fun!
mario

PS: Kudos go to Eric Shulman

PMario

unread,
Feb 28, 2019, 7:23:15 AM2/28/19
to TiddlyWiki
fixed a typo in the last post :)
Message has been deleted
Message has been deleted
Message has been deleted

Mohammad

unread,
Feb 28, 2019, 9:57:20 AM2/28/19
to TiddlyWiki
Thank you Mario!

and I appreciate your comment.

Cheers
Mohammad

Mohammad

unread,
Feb 28, 2019, 10:03:13 AM2/28/19
to TiddlyWiki
Mario,
 I forgot to say, I am using the below template to have macro visible


--Mohammad


On Thursday, February 28, 2019 at 3:51:14 PM UTC+3:30, PMario wrote:

Mohammad

unread,
Feb 28, 2019, 10:16:51 AM2/28/19
to TiddlyWiki
Josiah,
 I can add a simple logic as whole word option but here whole word means a word separated from other part of the text by two spaces.

The problem is, it will fail to distinguish punctuation. So, it can find " test " but not " test!".

Anyway, snr can do many search and replace jobs, but one needs to be careful like the example you gave above!
he in she, shell, shelf, ...

If TW got a Javascript replace function, the code will be much simpler, if not using current TW features the logic will be
difficult to make whole words search.

--Mohammad

On Thursday, February 28, 2019 at 6:19:57 PM UTC+3:30, @TiddlyTweeter wrote:
Mohammad

Really excellent. Its working well and I haven't been able to get it to fail. It will have huge use I am sure.

I do think an issue is UNDO.

Let me show why ...

On a balmy day SHE took advice that HE needed to walk.

If we replace "SHE" with "HE" we get ...

On a balmy day HE took advice that HE needed to walk.

But it can't easily be reversed  just switching "find" & "replace" terms as you'd get ...

On a balmy day SHE took advice that SHE needed to walk.

I think the simplest way to deal with this is that a Tiddler under s-n-r is cloned to a state tiddler before changes occur that can be RESTORED.

What do you think?

Best wishes
Josiah



Could the tiddler/s you working on be 


On Thursday, 28 February 2019 07:54:26 UTC+1, Mohammad wrote:
Announcement: SNR

This is a simple script, search-n-replcae (snr) to search in tiddler contents (including all fields) for a searchValue and replace them with replaceValue.

See the original post here!





--Mohammad

Mohammad

unread,
Feb 28, 2019, 10:19:10 AM2/28/19
to TiddlyWiki
Yes, an undo function is possible to not overwrite the original tiddlers.
But I am afraid duplication tiddlers in this way will result in huge file size.

--Mohammad

On Thursday, February 28, 2019 at 6:19:57 PM UTC+3:30, @TiddlyTweeter wrote:
Mohammad

Really excellent. Its working well and I haven't been able to get it to fail. It will have huge use I am sure.

I do think an issue is UNDO.

Let me show why ...

On a balmy day SHE took advice that HE needed to walk.

If we replace "SHE" with "HE" we get ...

On a balmy day HE took advice that HE needed to walk.

But it can't easily be reversed  just switching "find" & "replace" terms as you'd get ...

On a balmy day SHE took advice that SHE needed to walk.

I think the simplest way to deal with this is that a Tiddler under s-n-r is cloned to a state tiddler before changes occur that can be RESTORED.

What do you think?

Best wishes
Josiah



Could the tiddler/s you working on be 

On Thursday, 28 February 2019 07:54:26 UTC+1, Mohammad wrote:
Announcement: SNR

This is a simple script, search-n-replcae (snr) to search in tiddler contents (including all fields) for a searchValue and replace them with replaceValue.

See the original post here!





--Mohammad

Diego Mesa

unread,
Feb 28, 2019, 10:26:50 AM2/28/19
to TiddlyWiki
In my mind, an "undo function" is synonymous with "version control". 
Message has been deleted

Mohammad

unread,
Feb 28, 2019, 10:49:53 AM2/28/19
to TiddlyWiki
Hi Diego,
 That's absolutely correct! and seems the safer solution.

--Mohammad
Message has been deleted

Mohammad

unread,
Feb 28, 2019, 11:07:40 AM2/28/19
to TiddlyWiki
Josiah,
 Have a look ate TW roadmap:


It gives hope for the feature we like!

--Mohammad

On Thursday, February 28, 2019 at 7:24:17 PM UTC+3:30, @TiddlyTweeter wrote:
Diego Mesa wrote:
In my mind, an "undo function" is synonymous with "version control". 

I agree. Kinda. Though likely more short term than normal VC.

Mohammad wrote:
But I am afraid duplication tiddlers in this way will result in huge file size.

I agree it would if it were persistent. I'm thinking more of cloning of just a "start version" that persists only as long as you doing the replace. That when you finish and happy ts deleted?

Best wishes
Josiah
Message has been deleted

Mohammad

unread,
Feb 28, 2019, 12:46:57 PM2/28/19
to TiddlyWiki
Josiah,
 With current situation, my answer is unfortunately NO!
But the other macro I have developed can find and do partial transclusion



So, a mix of these two can do the job, but needs extra programming to work together.

--Mohammad


On Thursday, February 28, 2019 at 9:00:25 PM UTC+3:30, @TiddlyTweeter wrote:
This is such a good tool Mohammad! 

I ask a final, probably, very difficult, question ...

Can we match CARRIAGE RETURNS? And can we INSERT them in replacement strings?

Here is a simple example ...



<pre>The man was on the burning deck
His light was dim
And he got bogged.
</pre>




To do that you need to replace the HTML tags and remove excess carriage returns and replace with WikiText and insert carriage returns to get...

```
The man was on the burning deck
His light was dim
And he got bogged.
```

Could this be possible?

Just wondering
Best wishes
Josiah

BurningTreeC

unread,
Feb 28, 2019, 1:42:40 PM2/28/19
to TiddlyWiki
This is such a good tool Mohammad! 

I ask a final, probably, very difficult, question ...

Can we match CARRIAGE RETURNS? And can we INSERT them in replacement strings?

Hi Josiah, an sorry Mohammad, it can handle carriage returns, too.

Just make the inputs where you write the text to replace and the new text with textareas ... give those edit-text widgets the tag="textarea" attribute ... then play around with new lines
Message has been deleted

BurningTreeC

unread,
Feb 28, 2019, 3:00:20 PM2/28/19
to TiddlyWiki
BTC

That is good news!! 

Rather than have a text area could it be done via substitution? 

No that's too complicated I'm not even going to give the try a thought ;)

 
I'm thinking of the complexity of the interface to have a text area just for that. 
For instance could, with the example I gave, this work through the simple input fields ?...

Search ...
\r\r<pre>

Replace ...
\r```\r

Just thinking out loud
Best wishes
Josiah

David Gifford

unread,
Feb 28, 2019, 4:01:35 PM2/28/19
to TiddlyWiki
Oh, sorry, I came here because I thought you wrote "ASMR" plugin...

On Thursday, February 28, 2019 at 12:54:26 AM UTC-6, Mohammad wrote:
Announcement: SNR

This is a simple script, search-n-replcae (snr) to search in tiddler contents (including all fields) for a searchValue and replace them with replaceValue.

See the original post here!





--Mohammad

Thomas Elmiger

unread,
Feb 28, 2019, 4:49:19 PM2/28/19
to TiddlyWiki
Hi Mohammad, thank you, that looks very useful!

Regarding carriage (really?) returns, Josiah and BTC,

I would prefer a textarea that looks like an input field – but with the option to expand when needed. Like so:

<$edit-text tiddler="test" field="demo" tag="textarea" rows="1" autoHeight="yes" minHeight="1.75em"/>

Simple enough?

All the best,
Thomas

BurningTreeC

unread,
Feb 28, 2019, 5:56:41 PM2/28/19
to TiddlyWiki
Hi Mohammad, thank you, that looks very useful!

Regarding carriage (really?) returns, Josiah and BTC,

I would prefer a textarea that looks like an input field – but with the option to expand when needed. Like so:

<$edit-text tiddler="test" field="demo" tag="textarea" rows="1" autoHeight="yes" minHeight="1.75em"/>

Oh yes, that's better. I found it's a problem not knowing if there are newlines in a seamingly empty textarea or not 

Mohammad

unread,
Mar 1, 2019, 1:26:02 AM3/1/19
to TiddlyWiki
Hello Thomas,
 Many thanks for your feedback. The problem is with snr as it uses some filter operators not simply work with with regexp expression, if so
I personally prefer to have \t, \n, ... and like that in search area!

Cheers
Mohammad

Mohammad

unread,
Mar 2, 2019, 3:54:00 PM3/2/19
to TiddlyWiki
SNR2 announced.

SNR2 uses a small JS macro and can do search and replace in tiddler fields.
It is a flexible tool can perform search-n-replace in case-sensitive, case-insensitive
wholewords, ... manner

It also accept sregular expression so from this point of view is really powerful.
It can search for carriage return using \n  (asked by other users) and much more ...


See original post


SNR2 will be merged into Tiddler Commander.

Best regards
--Mohammad
Message has been deleted

Mohammad

unread,
Mar 3, 2019, 6:37:14 AM3/3/19
to TiddlyWiki
Many Thanks!
Yes, I checked and it seems `>` break the codes!
I will have look to see how I can fixed it.

Cheers
Mohammad

On Sunday, March 3, 2019 at 2:43:54 PM UTC+3:30, @TiddlyTweeter wrote:
Ciao Mohammad

I'll do lots of testing of SNR2 on  http://kookma.tiddlyspot.com/  and report back. It looks very good and very powerful!

The is one BUG I found already. You can easily see it. Just enter ">" in the search box and it breaks the script like this...

{4F3AC098-4005-4D1D-B466-3ECFDD1B3F90}.png.jpg

Best wishes
Josiah
Message has been deleted

Mohammad

unread,
Mar 4, 2019, 7:03:08 AM3/4/19
to TiddlyWiki
Hi Josiah

Many thanks. I will upload a new update.
I fixed the issue. I appreciate your time for evaluating the code and welcome your write up.

Best
Mohammad

Message has been deleted
Message has been deleted
Message has been deleted

Mohammad

unread,
Mar 4, 2019, 3:18:50 PM3/4/19
to TiddlyWiki
Hello Josiah,
 Thank you for your comments!
SNR2 simply use the JS replace function. So, everything is possible with JS replace theoretically should be possible with SNR2.

By the way I will investigate the case! 

--Mohammad

On Monday, March 4, 2019 at 11:06:10 PM UTC+3:30, @TiddlyTweeter wrote:
This is a footnote. Just FYI.

Regular Expressions are one of the few things I understand well. 

So IF anything I write about them seems difficult to understand please ask for clarification.

Best wishes
Josiah

Message has been deleted

Mohammad

unread,
Mar 4, 2019, 3:54:46 PM3/4/19
to TiddlyWiki
Hi Josiah,
 Seems it is true, have a look at



--Mohammad
Message has been deleted
Message has been deleted

Mohammad

unread,
Mar 4, 2019, 4:29:16 PM3/4/19
to TiddlyWiki
Yes, I got it. I am experimenting to see how resolve the issue.
The same is true for tab character \t

--Mohammad


On Tuesday, March 5, 2019 at 12:48:57 AM UTC+3:30, @TiddlyTweeter wrote:
Mohammad

The issue  is simply that we can get the MATCH well. But on REPLACE we might need to INSERT additional things that MATCH can't do.

Hope this is clear!
Josiah
Message has been deleted
Message has been deleted

Mohammad

unread,
Mar 5, 2019, 2:53:20 PM3/5/19
to TiddlyWiki
Yes, It is possible!
Save and reload and it should works.

--Mohammad

On Tuesday, March 5, 2019 at 11:15:31 PM UTC+3:30, @TiddlyTweeter wrote:
Ciao Mohammad

I request that the  flag be enabled to be toggled ON.

In some ways it might be better if  mode were  the DEFAULT as its far less dangerous than  .

FYI, I found it quite easy to get it run using "m" flag just by changing the JS code from "g" to "m" ... like this ...

{A7EE5869-65BB-4ABE-9471-6E897410BED5}.png.jpg


Would this be possible?

Best wishes
Josiah 


Message has been deleted
Message has been deleted
Message has been deleted

Mohammad

unread,
Mar 6, 2019, 8:52:51 AM3/6/19
to TiddlyWiki
Many Thanks Josiah!

I would appreciate your time teaching us how to use regexp in Tiddlywiki and specially here with SNR2.
For the issue you mentioned, I could not find any solution and Jeremy comments did not help me to understand what I should do!

By the way, I thought it is better to choose the flag from interface, so I am adding

i: ignore case
g: find all (global match)
\b: whole words
m: multi line mode

So, user can select one or a mixed of them (of course \b is something separate here!)

I am not sure if m is useful here, but i and g are used frequently!

--Mohammad



On Wednesday, March 6, 2019 at 5:10:19 PM UTC+3:30, @TiddlyTweeter wrote:
Mohammad & all interested

I have done a lot of testing of "s-n-r2". It is very good!


DIFFERENCES FROM NORMAL REGEX

It differs from normal regular expressions in that TiddlyWiki needs to guard itself from eating itself.
 
It does this by needing the regex to be wrapped in Quote Marks.

The type of quote marks differ according to the use-case.

IF a tiddler only contains  '  then wrapping in  will work. And vice-versa.

IF a tiddler contains both   and  quotes then the match string is easiest enabled by wrapping it:  """regex""" .


LIMITS

1 - I have found NO WAY to safely make changes in a tiddler that contains the string  """ . Those 3 double quotes are likely protecting TW from itself but the regex can't currently cope with them.
 
That rules out safe s-n-r via regex in many macros.
 
2 -  SUBSTITUTION. Mohammad and I found out that there is currently no easy way to get Control Codes into output replaces. This is problematic as, for instance, we need to replace  <pre>  with  \n```\n . BUT in replace the "\n" is only treated as a literal.

Best wishes
Josiah

Mohammad

unread,
Mar 6, 2019, 8:53:29 AM3/6/19
to TiddlyWiki
Josiah!
 I appreciate if you put these comments in few tiddlers to added as help to SNR2.

Mohammad

unread,
Mar 6, 2019, 8:56:15 AM3/6/19
to TiddlyWiki
One more comment:

I am not good at regexp, but I think snr should follow what is reule/standard in Tiddlywiki, so
I think we need to change the code to act as the current regexp operator (https://tiddlywiki.com/#regexp%20Operator)

So, please let me know, if snr has any conflict with regexp or pass TW red lines!
Message has been deleted

Mohammad

unread,
Mar 6, 2019, 9:15:06 AM3/6/19
to TiddlyWiki
Okay!
 I am achanging the interface and will upload a new update within an hour.

--Mohammad

On Wednesday, March 6, 2019 at 5:29:59 PM UTC+3:30, @TiddlyTweeter wrote:
M,

In particular, multi-line mode is excellent! Great news!

It allows step by step changes.

J.

Mohammad

unread,
Mar 6, 2019, 10:06:47 AM3/6/19
to TiddlyWiki
Announcement: SNR2

Update: Mar 6th, 2019

Changes:
  • regexp flags added (g, m, i)
  • whole words search added
  • minor issues fixed

This tool has amazing feature: You can search no only the text filed but also tags, caption, creator, ... and any other fields
You can even search and replace in tiddler title (in this case a new tiddler will be created!)

Please send you feedback and comments here!


------

Announcement: SNR2

This is a simple script, search-n-replcae (snr2) to search in tiddler contents (including all fields) for a searchValue and replace them with replaceValue.

Demo:
http://kookma.tiddlyspot.com/    (temporarily site )
Message has been deleted
Message has been deleted

Jeremy Ruston

unread,
Mar 6, 2019, 11:56:31 AM3/6/19
to tiddl...@googlegroups.com
Hi Mohammad

Sorry I missed your question in the thread. If you're trying to get a control character into a string for use in a regexp, you can use a construction like this:

<$codeblock code={{{ [[%0a]decodeuricomponent[]addprefix[a]addsuffix[b]] }}}/>

The idea is to express the character in the % notation used by URIs, and then use the decodeuricomponent filter operator to convert it into the corresponding character. In this case, $0a corresponds to the newline character (ascii code 10).

Best wishes

--
You received this message because you are subscribed to the Google Groups "TiddlyWiki" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tiddlywiki+...@googlegroups.com.
To post to this group, send email to tiddl...@googlegroups.com.
Visit this group at https://groups.google.com/group/tiddlywiki.
To view this discussion on the web visit https://groups.google.com/d/msgid/tiddlywiki/4aacaf40-2a40-43f2-a5ce-37a0ad33a1dc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mohammad

unread,
Mar 6, 2019, 12:31:23 PM3/6/19
to TiddlyWiki
Many thanks Jeremy!

It seems I should read and process the replaceValue first and then pass it to SNR macro!

By the way, I think this way I can handle control characters.

Best wishes
Mohammad

Mohammad

unread,
Mar 6, 2019, 1:15:27 PM3/6/19
to TiddlyWiki
Okay, Josiah!
I correct it!

--Mohammad

On Wednesday, March 6, 2019 at 7:59:11 PM UTC+3:30, @TiddlyTweeter wrote:
Mohammad,

Excellent!

An issue. You should not have Multi-line AND Global BOTH enabled! 

They are mutually exclusive.


{B8C55E38-08DD-4CC0-8A36-68B8ADA7732A}.png.jpg


Best wishes
Josiah

Mohammad

unread,
Mar 6, 2019, 4:49:32 PM3/6/19
to TiddlyWiki
Update: Mar 7th, 2019

Changes:
  • new interface
  • regexp flags can be selected among i, g,m but not g and m at the same time.
  • minor issues fixed
Demo:
http://kookma.tiddlyspot.com/    (temporarily site )

Mohammad

Diego Mesa

unread,
Mar 6, 2019, 6:48:32 PM3/6/19
to TiddlyWiki
Hey Mohammad,

I have the following strange behavior:

Mohammad

unread,
Mar 7, 2019, 1:53:08 AM3/7/19
to TiddlyWiki
Hello Diegi!
 I think the SNR does the job correctly!

May be Josiah can explain this in detail, but replace keep the parts in replaceValue and only add those characters word is not
available in the search value!

Here it actually change

This ---> This not
This ----> This not      }----> This not not


--Mohammad

Mohammad

unread,
Mar 7, 2019, 9:50:28 AM3/7/19
to TiddlyWiki
SNR is released as a plugin.

This thread is closed here and a new thread is started to discuss the SNR plugin.
To follow the discussion from new thread have a look at



I appreciate your feedback


--Mohammad
Message has been deleted

Diego Mesa

unread,
Mar 7, 2019, 1:45:29 PM3/7/19
to TiddlyWiki
Hey Josiah

It was the last point you made that really clarified it for me - the difference engine visualization confused m. Thanks!

Diego


On Thursday, March 7, 2019 at 11:59:36 AM UTC-6, @TiddlyTweeter wrote:
Diego,

You are right. It does look odd. But it IS working fine. The reason why it appears odd is ...

-- you run in "multi-line" mode and that means the match string always matches to first point that matches in the field. In this case the last two letters of This".

-- because your match ="is" and replace ="is not" no difference will show for "is" as you don't change it.

-- the "walking" green insertion is due to limits in the "difference" engine that doesn't always know on repeated words quite where to display the change. 
    It is actually happening directly next to "This".

Hope this is clear!
Josiah
Reply all
Reply to author
Forward
0 new messages