The “replace” function is one that shows up frequently in every programming language. The usage is simple but extremely powerful.
Usage
It follows a simple pattern.
- Main string
- Old Text
- New Text
Let’s start with an easy one.
Example:
replace('Hello World', 'World', 'Home')
will return
'Hello Home'
The function will replace the instances of “World” with the word “Home.”
But what if we have multiple instances o the word.
Example:
replace('Apple#Banana#Pear', '#', ',')
will return
'Apple,Banana,Pear'
The function will always replace all instances of the word in question.
For the last example, I want to give you a more usable scenario to use in your daily life.
Example:
replace('Manuel ## Gomes', '##', 'T.')
will return
'Manuel T. Gomes'
I’m giving this example on purpose because this can be quite a nice trick. You can have template strings with placeholders that you can replace with the final values. Think of examples like email templates where you have the full email in a string or a file and only want to “replace” it for the name of the person. You can have placeholders like “###1” and “###2” and then replace them for the real values and send.
Limitations
You can only use strings. It’s not possible to replacement of integer values, for example. You’ll get an error like this:
Example:
replace('Manuel ## Gomes', '##', 2)
will return the following error:
Unable to process template language expressions in action 'Compose_2' inputs at line '1' and column '2853': 'The template language function 'replace' expects its third parameter 'new string' to be a string. The provided value is of type 'Integer'. Please see https://aka.ms/logicexpressions#replace for usage details.'.
The same error will show up for any other types, so keep this in mind if you have variables and use them as parameters to the function.
Recommendations:
- Don’t build everything in the same formula. Ensure that you have only strings and keep the formula as simple as possible; even if you have to make some more steps with variables or composes, you’ll be able to debug and see if any value is not what you’re expecting.
- Don’t use this function inside other actions. Other actions have formula fields, but it makes it harder to debug. Do all the parsing before using it.
- Don’t nest it. It’s possible, and you won’t get an error, but it will be harder to debug also. Step by step, always.
- Please note that formulas may have localization-based differences. For example, you should write “replace” with, “,” separating each of the arguments, but if set your regional settings to Portugal, you should use “;” instead.
Sources:
Microsoft’s replace Function Reference.
Back to the Power Automate Function Reference.
Photo by Utopia By Cho on Unsplash
Hi,
Can the replace function be used to replace part of every string within an array in Power automate?
While reading an RSS Feed and filtering it to post a message with the selected items on a MS Teams channel, one of the string variables is at the origin RSS Feed an HTML code expression with a relative link in between. I do not have access to change the RSS feed.
Is there a way to replace the all the “href=’/web” in the array strings for “href=’https://www.site.org/web”sot that the links could work?
Thanks in advance for any help!
Hi JCL
It’s possible, yes. You need to do a “Apply to each” fetch each element of the array. Then you can do the replacement:
replace(item(),’href=’/web’,’href=\’https://www.site.org/web’)
Note that the quotes for the formula are ‘ and the ones you’re using are ’. In this case, it will work because the quotes you’re using are interpreted as a string, but be careful if you use ‘ in your strings.
Cheers
Manuel
Hi again,
Thanks for the reply.
Tested the replace(body(‘Filter_array’),’href=”/web’,’href=”https://www.site.org/web’) [used duplicated apostrophes to overcame the ‘ in the middle of the expressions).
I’m using the full array “body(‘Filter_array’)”,because if I choose the array item containing the HTML string to be partially replaced power automate creates an Apply to Reach fort the array item within another apply to reach for the array.
I must be doing something (very basic) in the wrong way…
Apply to Each to the Array filter result (previous step) by imputing onto the “Select an output from previous steps” the filtered array “body(‘Filter_array’)” and then use a” Compose” to do the replacement with the above expression.
The replacement only works if I convert the array to a string:
replace(string(body(‘Filter_array’)),’href=”/web’,’href=”https://www.site.org/web’)
However, how do I invoke the items of replaced Array in the next steps?
Do I need to convert it back to an array? I can only see a “Current item” outcome without knowing what type of object it is. And that gives me an error for the next step (publishing in MS Teams Channel)…
sigh…
Hey JCL,
I think you’re in the right way, but I need to see your Flow to see where it may be failing.
Do you mind emailing me a screenshot? You can offuscate anything that is proprietary.
Cheers!
Manuel
PS Sorry for typos…
No worries!! 😀
Any help? thanks in advance… I’ve tried everywhere for guidance already.
Hi JCL,
Don’t worry. We’ll get your Flow working.
Cheers
Manuel
You can find it here:
powerusers.microsoft.com/t5/Building-Flows/RSS-filtered-Array-item-replace/m-p/849735#M119013
Thanks in advance!
Hi Manuel, I’ve created a tool that your readers may find useful when it comes to the replace function.
It is a tool that generates nested replace function: https://mulquin.com/articles/power-automate-replace-generator
I hope you and others find it useful!
Great job Jacob! Indeed useful!
There is bit confision in article:
Limitations
You can only use strings. It’s not possible to replacement of integer values, for example. You’ll get an error like this:
Example:
replace(‘Manuel ## Gomes’, ‘##’, 2)
will return the following error
I disagree. Correct syntax (with 2 in quotation) :
replace(‘Manuel ## Gomes’, ‘##’, ‘2’)
Hi Jana,
Indeed you’re right; the correct syntax is with quotations.
My comment is because when you put something in quotations (even a number), it becomes a string. So 2 is a number while ‘2’ is a string that contains one character that is a number but could be something else.
I know it’s confusing, but I see this error all the time, so I wanted people to be aware and careful about it
Thanks!