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 Leaders.
May 31, 2024 edited

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

3 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 Leaders.
May 31, 2024 edited

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 Leaders.
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 Leaders.
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 Leaders.
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
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 Leaders.
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!

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
PREMIUM
TAGS
AUG Leaders

Atlassian Community Events