0

I have the following problem that I'm trying to solve with String.replace:

I have a random text that contains IDs of tickets in some system e.g. JIRA-123. I want to replace then with Markdown links like so:

text.replace(/(JIRA-\d+)/g, "[$1](example.com?id=$1)");

The problem I'm struggling with is that I want this operation to be idempotent i.e. if a given ID is already wrapped in the URL I want it to be ignored and I'm struggling to figure out a correct expression to achieve that.

Any help?

4
  • First of all, generateLink("$1") is not working. Commented Aug 31, 2020 at 13:16
  • 1
    Yes, so just follow the second linked thread: use a markdowned URL pattern (p1) and plain URL regex (p2) in an alternation, capture what you need to keep as is, and modify only what is matched and not captured (i.e. s.replace(/(p1)|p2/g, (x,y) => y ? x : process(x))) Commented Aug 31, 2020 at 13:32
  • @WiktorStribiżew Where does the ticket id /(JIRA-\d+)/g expression fit in this solution? Commented Aug 31, 2020 at 14:29
  • Okay, I think you meant that p2 a ticket pattern - I think I got it working, thanks :) Commented Aug 31, 2020 at 15:03

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.