Replacing a whole section in a note

An interesting question came up in a support request, and I’ve answered it there already but I’d like to post it here, too.

I have a note with a section that I would like to replace the content of, every time a Shortcuts workflow is run. Is that possible?

Yes, that’s doable using the Search And Replace In A Note action and it’s regular expressions feature. What you can do is:

  1. Add two Markdown comments to the note which act as boundaries for the section of the note you want replaced.

  2. Tell the action to replace the entire section while preserving the boundary comments using regex.

So, in your note, add the boundary comments (they don’t show up in Read mode):

%%replacer-start%%
%%replacer-end%%

Then configure the action. The search term is /(%%replacer-start%%).*(%%replacer-end%%)/gs , the replacement is $1[Text]$2 (where “[Text]” is whatever you want to place there. It’s just important that “$1” and “$2” exist because those are dynamic references to the boundary comments in the search term, they point to the text(s) in parentheses. Make sure to enable regex mode in the action:

Now the action removes everything including the boundaries, but keeps them in “$1” and “$2”, respectively, wraps your new text in them, and then inserts the whole thing.

By the way, the “title” of the boundary comments isn’t relevant, you can name them as you like. There can be more than one set in a note, too.

And there’s nothing wrong with keeping them short and sweet, e.g. %%1-start%% and %%1-end%% , %%2-start%% and %%2-end%% , etc.! (I just recommend not using any spaces in them, because it’s easy to introduce one space too many somewhere, and then things might break for no apparent reason.)

2 Likes

A post was split to a new topic: Question about “the dance” on iOS