Original question below but having done further testing is more fundamental
How can I use variables in the String replacement part e.g.
{{webresponse.body.name.replace("CHG","GHC")}} works
but VarOne=CHG and varTwo=GHC
{{webresponse.body.name.replace("{{varOne}}","{{varTwo}}")}} gives syntax errors.
How can I use variables in the replace part
Is is possible to do a string replacement on a smart variable that I have created in an automation script, using variables as the replacement string e.g.
Create Variable varLongStr
"Here is my number one text"
Create Variable varOne
"one"
Create Variable varTwo
"two"
and I want to modify varLongstr using something like
{{varLongStr}}.replace("{{varOne}}","{{varTwo}}")}}
but I can't get the syntax right
Hi @Rees, Ian
Short answer, please try this:
{{varSource.replace(varOne, varTwo)}}
Where the variables are defined as this:
For more information...
Some rule functions accept variable parameters and some will not. And even for those which work standalone, they may not work in other contexts, such as inside of an iterator. The only way to know for certain is experimentation.
Please also note:
You are using the replace() function, which takes text as parameters. Note in my example, there are no quotation marks in the variable definitions. The function call handles that as variables are text value type. The same is true when storing a regular expression on a variable, such as when using replaceAll() or match(). The key is to escape any reserved characters for the regular expressions.
Kind regards,
Bill
Wow, Thank you for this post. I was not aware of this. Thanks!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks Bill,
I wasn't sure what needed escaping so I tried all of the following and all failed so I am assuming that replace doesn't work with variable parameters and so will use another method
attempt1 = {{varSource.replace({{varOne}},{{varTwo}})}}
attempt2 = {{varSource.replace("{{varOne}}","{{varTwo}}")}}
attempt3 = {{varSource.replace(\"{{varOne}}\",\"{{varTwo}}\")}}
attempt4 = {{varSource.replace("\{\{varOne\}\}","\{\{varTwo\}\}")}}
attempt5 = {{varSource.replace(\{\{varOne\}\},\{\{varTwo\}\})}}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Rees, Ian please see my earlier example. Once inside of the double-curly brackets, there is no need to add additional ones.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello @Rees, Ian
Unfortunately, functions dont take variables as inputs. So you cannot use your variable inside replace or match or conditional or any other functions. Hope it helps. Thanks!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Join PM Evangelist, Axel Sooriah, & Product Ops. Consultant & Advisor, Jenny Wanger, as they unpack the role of product ops and discuss key takeaways in Atlassian’s ‘The Product Ops Mission’ guide. They’ll answer your questions live on May 6 at 9:00am PT.
Register here ⬇️Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.