# Regexreplace multiple strings with a loop?

**URL:** <https://connect.superhuman.com/t/regexreplace-multiple-strings-with-a-loop/47717>\
**Category:** Ask the Community\
**Created:** [April 2, 2024, 7:28am UTC](https://connect.superhuman.com/t/regexreplace-multiple-strings-with-a-loop/47717 "2024-04-02T07:28:46Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jon\_Dallas](https://yyz1.discourse-cdn.com/coda1/user_avatar/connect.superhuman.com/jon_dallas/32/30836_2.png) [@Jon\_Dallas](https://connect.superhuman.com/u/Jon_Dallas)\
**Post date:** [April 2, 2024, 7:28am UTC](https://connect.superhuman.com/t/regexreplace-multiple-strings-with-a-loop/47717/1 "2024-04-02T07:28:46Z")

</div>

Hello!

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

The goal is to use replace/substitute to wrap multiple words in a paragraph with brackets.

So, for example, after selecting “red” and “nose” in Column A, Column B would be "Rudolph the [red] nosed reindeer had a very shiny [nose].

I can use regexreplace() or substituteAll() for this, but only one word at a time. To wrap multiple words in brackets, I would need to nest multiple substituteAll()s. But I want this to be dynamic, i.e., I want it to work with a varying number of words selected.

So I think we need to use a loop that can iteratively/recursively wrap each word from the list in brackets. For this we can use formulaMap().

But this formula:

```auto
thisRow.[Words to Wrap in Brackets].FormulaMap(
  SubstituteAll(thisRow.[Original Paragraph],CurrentValue,"["+CurrentValue+"]")
)

```

produces this output:

> Rudolph the [red] nosed reindeer had a very shiny nose.,Rudolph the red [nose]d reindeer had a very shiny [nose].

Because it’s recursive, maybe we can use withName(), but I don’t know if withName can redefine already-named variables.

[Here](https://coda.io/d/_dPlwhJG7XCB/Regexreplace-multiple-strings-with-a-loop_suOU2)’s a test doc. Feel free to make a copy.

Thanks for your help!

---

<div class="post-metadata">

**Author:** ![Scott\_Collier-Weir](https://yyz1.discourse-cdn.com/coda1/user_avatar/connect.superhuman.com/scott_collier-weir/32/29677_2.png) [@Scott\_Collier-Weir](https://connect.superhuman.com/u/Scott_Collier-Weir)\
**Post date:** [April 2, 2024, 9:14pm UTC](https://connect.superhuman.com/t/regexreplace-multiple-strings-with-a-loop/47717/2 "2024-04-02T21:14:02Z")

</div>

I did this once a long time ago

I think the easiest way is that if every target you are after is a single word, simply Split(“ “) Which will turn your paragraph into a list of individual words, then run a forEach() Statement, and for each loop use a switch if statement that conditionally replaces the current value with your bracketed target value

Or by default return the current word if it’s not a target word.

Then join with a “ “

That will work, if the targets are after not individual words, you can do it with a button in a single helper column that recursively sets the value of the column, replaces the new word, and then resets it

Let me know if you need more help

---

<div class="post-metadata">

**Author:** ![Rickard\_Abraham](https://yyz1.discourse-cdn.com/coda1/user_avatar/connect.superhuman.com/rickard_abraham/32/35007_2.png) [@Rickard\_Abraham](https://connect.superhuman.com/u/Rickard_Abraham)\
**Post date:** [April 3, 2024, 5:54am UTC](https://connect.superhuman.com/t/regexreplace-multiple-strings-with-a-loop/47717/3 "2024-04-03T05:54:30Z")

</div>

Oh my god, I just found that Coda does have support for regex capturing groups.  
I’ve been trying `\1` since I’m used to Python, but apparently `$1` is the way to go, like in Javascript!

Anyway, this makes this problem much easier!

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

```

https://coda.io/embed/MVRa7tcuGD?viewMode=embedplay#_tuCRS/r78&view=full

---

<div class="post-metadata">

**Author:** ![Max\_OBrien](https://yyz1.discourse-cdn.com/coda1/user_avatar/connect.superhuman.com/max_obrien/32/49285_2.png) [@Max\_OBrien](https://connect.superhuman.com/u/Max_OBrien)\
**Post date:** [April 3, 2024, 6:06am UTC](https://connect.superhuman.com/t/regexreplace-multiple-strings-with-a-loop/47717/4 "2024-04-03T06:06:40Z")

</div>

the non-regex idiom is split(‘xxx’).join(‘yyy’) which replaces all instances of xxx with yyy.  
so you just chain these pairs together.

it may not be a fast (measured in micro-seconds) as regex, but its easier to remember.

---

<div class="post-metadata">

**Author:** ![Rickard\_Abraham](https://yyz1.discourse-cdn.com/coda1/user_avatar/connect.superhuman.com/rickard_abraham/32/35007_2.png) [@Rickard\_Abraham](https://connect.superhuman.com/u/Rickard_Abraham)\
**Post date:** [April 3, 2024, 6:23am UTC](https://connect.superhuman.com/t/regexreplace-multiple-strings-with-a-loop/47717/5 "2024-04-03T06:23:07Z")

</div>

That’s a pretty clever workaround! But I believe the new `SubstituteAll` replaces the need for that right?

---

<div class="post-metadata">

**Author:** ![Christiaan\_Huizer](https://yyz1.discourse-cdn.com/coda1/user_avatar/connect.superhuman.com/christiaan_huizer/32/28269_2.png) [@Christiaan\_Huizer](https://connect.superhuman.com/u/Christiaan_Huizer)\
**Post date:** [April 3, 2024, 8:56am UTC](https://connect.superhuman.com/t/regexreplace-multiple-strings-with-a-loop/47717/6 "2024-04-03T08:56:26Z")

</div>

a clever solution for this specific use case:

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

I wonder why @Jon_Dallas needs the , can you enlighten us?

---

<div class="post-metadata">

**Author:** ![joost\_mineur](https://yyz1.discourse-cdn.com/coda1/user_avatar/connect.superhuman.com/joost_mineur/32/30539_2.png) [@joost\_mineur](https://connect.superhuman.com/u/joost_mineur)\
**Post date:** [April 3, 2024, 12:00pm UTC](https://connect.superhuman.com/t/regexreplace-multiple-strings-with-a-loop/47717/7 "2024-04-03T12:00:21Z")

</div>

@Rickard_Abraham , @Max_OBrien ,

The RegEx from Rickard allows for multiple search and replaces in one operation, at least the add-brackets type operation. With The CFL functions like ReplaceAll you have to run as many iterations as there are search words and if you don’t work with a temp column you get the repeats of the original text, as mentioned earlier.

I think the RegEx the way it is presented by Rickard is one of those gems we every so often run into - thanks.

Greetings, Joost

---

<div class="post-metadata">

**Author:** ![Jon\_Dallas](https://yyz1.discourse-cdn.com/coda1/user_avatar/connect.superhuman.com/jon_dallas/32/30836_2.png) [@Jon\_Dallas](https://connect.superhuman.com/u/Jon_Dallas)\
**Post date:** [April 7, 2024, 5:36am UTC](https://connect.superhuman.com/t/regexreplace-multiple-strings-with-a-loop/47717/8 "2024-04-07T05:36:27Z")

</div>

Hey, thanks so much for the solutions ya’ll.

@Rickard_Abraham’s solution seems to be most well suited for my purposes here. Just gave it a try and it works perfectly. Really awesome find!

@Christiaan_Huizer, as for the specific use-case, I am building a Claude-based resume optimizer. The final resume will contain some fabricated information about the candidate. So I have a helper prompt determine which information was fabricated, then I will use this formula to wrap that information in brackets [like this] to indicate that it’s a placeholder.

---

<div class="post-metadata">

**Author:** ![system](https://yyz1.discourse-cdn.com/coda1/user_avatar/connect.superhuman.com/system/32/24484_2.png) [@system](https://connect.superhuman.com/u/system)\
**Post date:** [April 10, 2024, 5:36am UTC](https://connect.superhuman.com/t/regexreplace-multiple-strings-with-a-loop/47717/9 "2024-04-10T05:36:32Z")

</div>

Closed on April 10, 2024, 5:36am UTC

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.
