substituteAll at work

SubstituteAll() in Coda

The upgraded version of Substitute()

4 min readJan 15, 2024

--

It was the friendly and supportive Codan Eric_Koleda who announced this function on January 12, 2024. As in most cases the official and formal proza is a dry and not inspiring.

coda proza on SubstituteAll

Practical Applications

The function Substitute() is active on the first match in a string. Below the difference applied to numbers. The conversion from EU numbers to the default USA numbers is a returning concern in the Coda community. The substituteAll() looks like a RegexReplace() with the flag ‘global’ active. Like substitute, a Regex looks for the first match only, unless you instruct it to keep looking. Below how to convert numbers using SubstituteAll().

simple solution

Replacing text

We use the word ‘replace’ while we apply the function SubstituteAll() instead of Replace() which is also a Coda function. Replace is about a string replaced in a text based on the position of the string in the text. The position is the count of characters in the text as of a certain position (default zero), likewise you get via Find(). When we apply SubstituteAll() to replace words in a text, we do so regardless their position in the text. That is the fundamental difference with Replace() and other position based functions like Splice(). More info in an older blog.

Below you see what happens when you replace the word contract with CONTRACT.

substituteAll

In case you add a white space after ‘contract’ you solve one problem and generate a new one (I suggest you try it out).

Unlike Regex, the function does not search for a pattern, it searches for a string regardless the position in the text. You see how easily you run into trouble.

I would like to argue that makers better use Regex, maybe ask for a regex expert (and sorry to say, but AI is not yet good enough in most situations) to solve issues like in the above. In general I would appreciate a bit more love from Coda towards the doc side of the application. That brings me to the next point.

Style preservation — SubstituteAll won’t work

In the community a returning question relates to style preservation. The last contribution you find here. It is a split — join operation and avoids the usage of RegexReplace() which would be the better, but is also a more difficult to understand approach and has as downside that the style is gone.

If you’ve ever talked about regular expressions with another programmer, you’ve invariably heard this 1997 chestnut:

Some people, when confronted with a problem, think “I know, I’ll use regular expressions.” Now they have two problems.

The downside of Regex is that it flattens the outcome, your style is gone, which is also the case for Substitute() and SubstituteAll() Only with functions as Split() and Splice() you can preserve the style, which is of great value.

To preserve style, we need a different approach. How it looks you see below.

preserving style

Style preservation is only possible with a dedicated pack or applying the mentioned split() — join() operation.

´The screenshots come from the published doc.

My name is Christiaan and blog about Coda. Since the summer of 2023 often (but not only) about how to Coda with AI to support organisations dealing with texts and templates.

The latest Coda AI update was on Dec 7, 2023.

Why I focus on Coda AI you can read here: ⤵️

I hope you enjoyed this article. If you have questions feel free to reach out. Though this article is for free, my work (including advice) won’t be, but there is always room for a chat to see what can be done. You find my (for free) contributions to the Coda Community and on Twitter.

Coda comes with a set of building blocks ー like pages for infinite depth, tables that talk to each other, and buttons that take action inside or outside your doc ーso anyone can make a doc as powerful as an app (source).

Not to forget: the Coda Community provides great insights for free once you add a sample doc.

--

--

I write about Coda.io - AI and (HR )planning challenges. You find blogs for beginners and experienced makers. I publish about once per week. Welcome!