Regexreplace multiple strings in a loop with FORMATTING?

Hello!

Is it possible to use substituteAll() or regexreplace() recursively in a loop on the same original “haystack” text to bold certain parts using _bold()?

The goal is to use replace/substitute to bold (or any kind of format function) multiple words in a paragraph.

So, for example, given Columns A and B, this formula would produce Column C:

  • Column A: Rudolph the red nosed reindeer.
  • Column B: red, reindeer
  • Column C: Rudolph the red nosed reindeer.

Earlier in this thread, @Rickard_Abraham discovered that Coda does have support for regex capturing groups and we can use this formula to recursively wrap the chosen text in brackets:

thisRow.[Original Paragraph].RegexReplace("(" + thisRow.[Words to Wrap in Brackets].Join("|") + ")", "[$1]")

But this same formula doesn’t work with formatting functions like _highlight() or _bold().

Thanks for your help!

Oh, I remember pulling this off quite a while ago. Of course I wouldn’t find it now :sweat_smile:

I recently solved a somewhat similar thing. It was about replacing {{Named}} {{parameters}} with ready-to-Format() placeholders like {1} {2} etc. and applying proper values to those. And my solution preserved formatting. I have only recorded a very improvised video on that, which I shared with patrons, but I was too overwhelmed to make a proper tutorial out of it yet.

The short answer is, it’s only possible with some effort. I don’t remember if Substitute preserves formatting (probably not / not yet?) but RegexAnything functions for sure don’t.


P.S. Oh, I guess that’s it. Here I was replacing sought words with underlined versions of them.
RegexExtract isn’t a hidden formula anymore, and we didn’t have WithName back then, so today you can easily make it into a single formula.

When I read the title of your post, I thought you wanted to do replace-like operations on formatted text. So, I admit I spent way too much time thinking about this problem. Paul’s and BenLee’s approaches are both good, but there are some limitations. Check out this demo if you’re interested.

Open in new tab.

the demo looks great, but its read-only, so we cannot see the formulas being used.

1 Like

It’s set to comment-only, but you can still explore the formulas in the little embed window or open in a new tab. (also added this link to my first post)

1 Like