Issues with the replaceAll function. How can I remove special characters [, ], " from string?
Asked by John Izquierdo:

Hi,

I'm I'm trying to remove specific characters '\[', '\]', and '"' (quotation marks) from a 'value' in the description. Below is the value.

Additional Hardware Selected: \["laptop dock", "laptop", "keyboard", "mouse"\]

I'm first using create variable function in an automation to create a variable with the name **{{msformAddHardware}}** and the value **{{issue.description.match("Additional Hardware Selected: \*(.\*)")}}**.

Then, I'm using the Audit Log function with the smart-value **{{msformAddHardware.replaceAll("\[\\\\\[\\\\\]\\"\]", "")}}** but the Audit log shows the log function is empty.

My purpose for this is, in the automation, to use the created variable to set the value for a multi-select custom field.

Please help. Thanks!


Answers:
author: William Calesco
body: Hi John,  

I know that character [Classes](https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html) work for replaceAll().  

If you are getting this **"\[\\\\\[\\\\\]\\"\]"** (isn\`t there an extra " ?) from different parts of the text,  

try chainning the characters, for example:  

```
{{issue.summary.replaceAll("[\\]", "").replaceAll("\", "")}}
```

and so on. Start with the first character, test, and then progress adding the others.

comments: author: John Izquierdo
body: Hi, William

The Log Action was returning the error below.
> Failed to get value for msformAddHardware.replaceall("\[\\\\\]", ""): {{msformAddHardware.replaceall("\[\\\\\]", "")}}

I ended up using: value **{{msformAddHardware.replaceAll("\[\\\\\[\\\\\]\\"\]", "")}}** to create a new variable "addHard". Then, created a new variable "selectHardware" with the value **{{addHard.trim()}}**.

I might have over done it with all the created variables but nonetheless.

Then, with in the "Edit" function I used the following JSON.

```
{
"fields": {
"customfield_12487": [
{{#selectHardware.split(",")}}
{"value": "{{.}}"}{{^last}},{{/last}}
{{/}}
]
}
}
```


author: Kalyan Sattaluri
body: Hello [@John Izquierdo](/t5/user/viewprofilepage/user-id/4993587)

Yes, as [@William Calesco](/t5/user/viewprofilepage/user-id/5447291) has mentioned, try chaining the "replace" function to replace one at a time.

Once you have a clean list separated by commas in your variable (which is still text), please check the method demonstrated by [@Bill Sheboy](/t5/user/viewprofilepage/user-id/1685313) in below post on how to format your text to JSON so you can set/update your field accordingly.

<https://community.atlassian.com/t5/Automation-questions/Converting-list-to-JSON-object/qaq-p/2551363>

comments: author: John Izquierdo
body: Thanks, Kalyan!

I was still confused reading that post (still kind of new to Jira and getting familiar with smart-values but don't know a lick of JSON), thankfully we're working with some devs from OnRamp who provided the same solution and was able to explain it/how it works.


author: Kalyan Sattaluri
body: Yes, It takes a couple of tries to absorb how to iterate a list and format the JSON to get it so you can update corresponding fields.

And that method of [@Bill Sheboy](/t5/user/viewprofilepage/user-id/1685313) is one of my absolute favorites I have ever seen. Its just such a simple yet, out of the box solution that my mind was blown away and i am still impressed, lol.

For reference, if you look at below Jira KB article on "how else" it could have been achieved, its just confusing and feels round about. That's what i admire about that solution.

<https://confluence.atlassian.com/jirakb/converting-a-csv-string-to-a-json-object-array-when-json-smart-value-functions-do-not-work-in-an-automation-rule-1189506509.html>

BTW, can I please ask you to accept [@William Calesco](/t5/user/viewprofilepage/user-id/5447291) answer as well, his solution was the correct one so folks researching in the future can benefit by quickly getting to the answer. Thanks!


author: John Izquierdo
body: Ah! I might have been thinking of a different community post with a similar question to mine. I didn't try, but still don't fully understand the article you linked, I'll have to stare at it for a while and test it out and fail a couple of time. lol

I explained what method I ended up using in a reply to [@William Calesco](/t5/user/viewprofilepage/user-id/5447291)... I might have used the roundabout way of doing it tbh.

Once I figure out and successfully use the method provided in that article I will be sure to update my automation so it doesn't feel like a mess.

Thanks again!


Tags: log

smart-value