Tinderbox Forum

Find and replace in Name and Text

It’s a simple operation, but I can’t find a way to do it. I tried with

$MyString = $MyString.replace(“Hello”,“Goodbye”)

But can’t find a way to make it work.

Several possible issues. Firstly, and this may be because you didn’t format you post as code, but you have the wrong type of quotes. You are using ‘curly’ typographic quotes where action code only allows string delimiting quotes to be straight single quotes. If I use:

$MyString = $MyString.replace("Hello","Goodbye")

it works for me. Bear in mind the matchString pattern in .replace(matchString,replaceString) is case-sensitive so your code is only matching ‘Hello’ but not ‘hello’. We can make matchString match H and h, like so:

$MyString = $MyString.replace("[hH]ello","Goodbye")

Note we can’t make the replaceString match initial-letter case of matchString, i.e. replacing ‘Hello’ with "Goodbye’ but ‘hello’ with ‘goodbye’. That would need two chained operations:

$MyString = $MyString.replace("Hello","Goodbye").replace("hello","goodbye")

Thanks, as usual Mark!

I found the problem. The query was wrong…

But something as usual as a global Find and Replace in Name and Text, should have been easier to do, no?

Tinderbox isn’t a word processor. $Text.replace() and $Name.replace() applied to a doc-scope selection or agent gives a global replace. What is the specific problem?

But we use it a lot for writing!

I can live with $Text.replace() and $Name.replace(). No problem.

But why there is a Find and replace item in the Edit menu, and when I use it I have access only to a Find for name and text?

But why there is a Find and replace item in the Edit menu

Because it’s available in the text pane.

Document-level Find and Replace in Tinderbox text is complex, because (a) some people expect the scope to include all attributes, while others don’t, (b) if it did include all attributes, you could destroy a document with a small mistake, and ( c) each operation on $Text requires every link to be adjusted, which makes document-level find and replace slow.

The document-level Find provides a tool for searching Name, Text, and any User attribute.

If you need a more elaborate Find, make an agent; it scarcely takes longer to make an agent than to bring up the Find bar.

2 Likes

Ok, I will use the agent. Thanks.