Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

String replace on a variable in an automation script

Rees, Ian
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Champions.
May 31, 2024

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

 

2 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

4 votes
Answer accepted
Bill Sheboy
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Champions.
May 31, 2024

Hi @Rees, Ian 

Short answer, please try this:

{{varSource.replace(varOne, varTwo)}}

Where the variables are defined as this:

  • action: create variable
    • name: varSource
    • smart value: Here is my number one text
  • action: create variable
    • name: varOne
    • smart value: one
  • action: create variable
    • name: varTwo
    • smart value: two

 

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

Kalyan Sattaluri
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Champions.
May 31, 2024

Wow, Thank you for this post. I was not aware of this. Thanks!

Like Bill Sheboy likes this
Rees, Ian
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Champions.
June 7, 2024

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\}\})}}

 

Bill Sheboy
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Champions.
June 7, 2024

@Rees, Ian please see my earlier example.  Once inside of the double-curly brackets, there is no need to add additional ones.

Like Jason M_ likes this
Ludwig Einicke
May 22, 2026

Another comment from the future here:

Normally replace(...) accepts Variables. Here are some cases where I have encountered issues and my steps on fixing them

0) General Tips for Debugging

Log action and/or using {{debug}}...{{/}} are your friends for this. Thinking "this should be the output/input" and KNOWING it specifically because it gets posted in a Log are two VERY different things.

1) "Communication disrupted by different location"

Problem: There CAN be issues when your variable and the action using the variable have different "logical" locations. one example of this would be defining your variable in the "main" function while the "recipient" of the variable is inside a conditional branch.

Possible Workaround: Using a Lookup Table has usually done the Trick for me. It might need some fiddeling around with flatten() and transferring Table Values into Variables for a specific branch but usually i could get the function to properly "read" my inputs by playing around experimenting with this in mind. 

2) Variable doesn't get set up correctly

Problem: Sometimes the input itself doesn't get read right. This only happens to me when i try to set up Variables inside a conditional branch before any variables have been set up inside the main branch.

A classical case would be:

trigger (any)

>if-branch case 1: Customfield A is empty
>>create Variable
>>Name: myVar, Value: {{one}}

>if-branch case 2: Customfield A is not empty
>>create Variable
>>Name: myVar, Value {{two}}

log Action: {{myVar}}

The Log will (at least in the cases i encountered this issue) be empty.

Workaround: "Initialise" the Variable (possibly like this):

trigger (any)

create Variable
Name: myVar, Value: 1


>if-branch case 1: Customfield A is empty
[...]

log Action: {{myVar}}

Now it will show a value (not only "1" but also the case-sensitive smart values).

Added benefit: you can now check if your variable got set up correctly (myVar different from 1) or if some kind of error occured (myVar equals 1).

Sidenote: LookupObjects do set up a variable so they fix the issue aswell if used before the branch.

3) General Benefits of a Lookup Table

The pretty recently added Lookup Table togehter with .get() works especially well for me when it comes to using it inside of functions.

Example fit to your initial case:

1) create lookup table

- Name myTable
- (first Row) Name: first | Value: "{{one}}"  <-- make sure this is plain Text
- (second Row) Name: second | Value: "{{two}}"  <-- make sure this is plain Text

2) use the lookup table entries in another action

{{varSource.replace(myTable.get(first), myTable.get(second))}}

Interestingly Í have encountered cases, where i specifically needed to use the .get() operator, when outside of replace() and similar "variable accepting functions" a casual mytable.first smart obnject would suffice. I guess .get() helps to making the "hi, i want you to use a variable"-part more explicit. 

 

@Bill Sheboy have you had any similar encounters or troubles? I would be interested in your take on those.

@Rees, Ian I hope this helps you and other people encountering the same problem.

Kind regards,

Ludwig

Evie Z_
Community Manager
Community Managers are Atlassian Team members who specifically run and moderate Atlassian communities. Feel free to say hello!
May 22, 2026

@Ludwig Einicke 

This topic is now closed as the discussion has become outdated. If you have more questions or want to continue the conversation, feel free to start a new topic. For more details on why we close older threads, check out our Rules of engagement - Atlassian Community .

Thank you for your understanding!

0 votes
Kalyan Sattaluri
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Champions.
May 31, 2024

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!

DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
PREMIUM
TAGS
AUG Leaders

Atlassian Community Events