Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Please add more examples of using Flow Transform Replace (Regular Expression) syntax

John Palmer
Tera Expert

I could use a lot more examples of using the Flow Designer Transform Replace features. It took me far too long today to figure out the correct syntax for "get first two characters, add a dot, get 3rd char, discard the rest, because I didn't understand how the Transform Replace used the Capture Group syntax. It wasn't until I had worked out how to do this with an Inline Script (which I avoid using because if you add, remove or move a step, you have to update all the  affected inline script 'fd_data' references, which you may not remember are there!

In any event after I accomplished this with the inline script, I realized that the Transform Replace probably used the same (new to me) method to refer to the Capture Groups (eg $1, $2 etc). so, to provide one or two examples for the community, this is what worked for me:
goal: convert "253000'' to "25.3" :

var objClass = fd_data._6__for_each.item.ObjectClass;

var regex = /(..)(.)(.*)$/;

// capture the 1st 2 chars, insert a dot, then the 3rd char, eg "253000" becomes "25.3"

// (discard the remaining chars, eg the $3 match)

return objClass.replace(regex, '$1.$2');

So then I tried the Transform Replace with:
Regex:
(..)(.)(.*)$

Replace:
$1.$2    

JohnPalmer_0-1734127610213.png


Later today, I needed to "remove leading 0's" from  alpha-numeric text (numbers or letters with leading 0's:
so:

Goal: "0006450" becomes "6450":

JohnPalmer_1-1734127750148.png

the Replace is left empty, so what matches above gets replaced with null, 
Not sure I understood the RegEx completely but:
^0+(?!$)

0 or more zeros at the start, but not the last char at the end ( so "00000000" becomes "0")

 

Please add your Transform Replace examples!

0 REPLIES 0