How to replace text in Power Automate
Use replace(text, oldText, newText). It changes every match and is case-sensitive. To remove text, use an empty string as the new text.
What is the syntax of the replace() function?
The function takes three values: the text to search, the text to find and the text to put in its place. Example: replace('Hello World', 'World', 'Team') returns Hello Team.
Add the expression in a Compose action. To use a column value, write replace(triggerOutputs()?['body/Title'], 'old', 'new').
Does replace() change every match or only the first?
It changes every match. replace('a-b-c', '-', '_') returns a_b_c.
There is no option to change only the first match. To do that, split the text with split(), edit the first part, and join the pieces again.
How do you remove text instead of replacing it?
Pass an empty string as the third value: replace('INV-1042', 'INV-', '') returns 1042.
To remove spaces, use replace(variables('text'), ' ', ''). To trim spaces at the ends only, use trim() instead.
Is replace() case-sensitive?
Yes. replace('Invoice', 'invoice', 'Bill') changes nothing, because the case differs.
For a case-free match, lower the case of the source first: replace(toLower(variables('text')), 'invoice', 'bill'). The result is all lowercase. Use toUpper() the same way.
How do you replace a line break or an apostrophe?
- Line break: replace(variables('text'), decodeUriComponent('%0A'), ' ').
- Windows line break: replace the carriage return too, with decodeUriComponent('%0D%0A').
- Apostrophe: inside an expression, write two single quotes: replace(variables('text'), '''', '').
- Non-breaking space: replace(variables('text'), decodeUriComponent('%C2%A0'), ' ').
Why does replace() fail on an empty column?
If the value is null, replace() returns an error such as 'Unable to process template language expressions'. An empty SharePoint column often returns null.
Wrap the value in coalesce(): replace(coalesce(triggerOutputs()?['body/Title'], ''), 'old', 'new'). The empty string stands in for null. For the related null error, see expected String but got Null.
Does replace() support wildcards or regular expressions?
No. replace() matches the exact text you give it.
For a pattern, use one of these options: chain several replace() calls, or use a connector action that supports regular expressions. Chained calls work well for a short list of known values.
FAQ
Can I chain several replace() calls?
Yes. Put one replace() inside another: replace(replace(text, 'a', 'b'), 'c', 'd'). Each call works on the result of the inner call.
Does replace() work on numbers?
It needs text. Convert a number first with string(), for example replace(string(1234.5), '.', ',').
Is there a replace action in the designer?
No. Use the function in a Compose, Set variable or other action field. The Expression tab holds the function list.
Sources
- Reference guide to workflow expression functions in Azure Logic Apps and Power Automate
- Power Automate: replace function