Regular expressions are very useful for extracting and replacing information from any text but Power Automate does not support Regex(Regular expressions). This blog will provide information on how to use it in Power Automate.
Third party connectors exist which can perform Regular Expressions on behalf of Power Automate, but here, we will focus on how to execute regular expression actions for free, within Power Automate.
As we know that JavaScript supports regex and it can be accessed via Office Scripts in Excel Online. It is possible to pass parameters into, and out of Office Scripts. So, Excel Online can be used as a host for getting access to JavaScript functionality.
Let’s start
I want to extract some string from the email subject followed by some specific word.
Steps:


“Regex Match” script accepts three parameters:
a. Create a blank excel file in one drive and open it. You can see Automate menu as shown in the below picture.

By selecting All Scripts, one can see all the scripts. To create a new one click on New Script option.
Below is the script for the Regex match. Create a new script using it and save the file. Then run the flow and check the result.
function main
(
workbook: ExcelScript.Workbook, inputString: string, pattern: string, flags: string
): Array<string> {
regExp = new RegExp(pattern, flags);let
let matches: Array<string> = inputString.match(regExp);
if (matches) {
return matches;
} else {
return [];
}
}
One can use any word in regex as a matching word as per the requirement or can also use the new regex pattern to fulfill the need.
Let’s take an example that would be difficult to achieve with standard Power Automate actions. The string has too many space characters, and I’d like to replace them with a single space.
it is very simple with the regular expression pattern ss+:
To substitute text with another text below is the office script:
function main
(workbook: ExcelScript.Workbook,
regexPattern: string,
regexFlags: string,
searchString?: string,
replaceString?: string,
): string {
if(typeof (searchString) === 'undefined' || searchString == "" || searchString == null)
return ""
if (typeof (replaceString) === 'undefined') { replaceString = ''; }
let re = new RegExp(regexPattern, regexFlags)
return searchString.replace(re, replaceString)
}
Save it in excel office script and use it in the Power Automate like below and check the result.

I hope you found this blog useful. Regular expressions are a powerful tool for working with text, and Power Automate makes it easy to create regular expressions. We hope this article has been helpful, to learn more contact us today.
======
Plus read
Download file from Power Apps to local dynamically
Follow us