How to handle escape character in replaceAll

KB April 3, 2024

Hi, I want to escape all mardown characters in a text to post it on slack without formatting.

 

For this I use

 

{{value.replaceAll("([*_{}\[\]()#\+\-\.!|])", "\\$1")}}

The problem is, that this escapes the $1 so the match is replaced by "$1" instead of "\actualmatch". So looking like

$1strike$1

I tried escaping up to 4 backslashes. With 4 the replace works works but it puts out 

\-strike\-

 Would be great to learn how to escape in a way that slack does the right thing.

1 answer

0 votes
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.
April 3, 2024

Hi @KB 

When you want to strip out the markdown, please try the text function:

{{issue.someFieldWithMarkdown.text}}

And also...

The documentation for automation use of regular expressions states they are "based on Java's Pattern class", but it does not state what actually is / is not supported.  Several community posts indicate variations in behavior.

I recommend always starting with the simplest expression that can possibly work, testing thoroughly, and incrementally adding to the expression to handle any edge cases.

Kind regards,
Bill

KB April 3, 2024

Thanks Bill.

I dont necessarily want to remove the markdown signs, as they are not intended as mardown, but slack interprets them as such, this is why I'd like to escape them properly.

Also I think the regex is quite simple, but the replace part does not seem to work as intended.

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.
April 4, 2024

Considering the documentation disclaimer, the only way to confirm what works is by experimenting...

After I pondered a bit, I remembered I solved a similar challenge last year with created variables:

  • action: create variable
    • name: varRegularExpression
    • smart value: ([*_{}\[\]()#\+\-\.!|])
  • action: create variable
    • name: varReplacement
    • smart value: \\$1
  • action: some action needing the replaceAll with this:
    • {{issue.someField.replaceAll(varRegularExpression, varReplacement)}}

The "trick" is the order of evaluation.  The variables are fully defined before attempted use in the replaceAll() call.

 

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
STANDARD
PERMISSIONS LEVEL
Site Admin
TAGS
AUG Leaders

Atlassian Community Events