How do I replace a string in the 2nd occurrence? lets say for example I want to replace the 2nd [/CODE]. How can I do this cause my workflow is replacing the 1st occurrence which I don’t want
The “match” string is a regex, so all special characters must be escaped (so \[\/CODE\] instead of [/CODE]
The replacement is a function instead of a static string. The function uses the match and offset parameters. The function body is: offset > $json.data.indexOf('[/CODE]') ? 'Link5\n[/CODE]' : match
This chooses (ternary / ? operator) based on whether the current offset is past the first occurrence indexOf() of [/CODE] (so it would change ANY match after the first one)
true (ternary arg before :) → replace with specified text → 'Link5\n[/CODE]'
Note: This value could be assembled from expression references. e.g. $json.newLink + '\n[/CODE]'
false (ternary arg after :) → just re-insert the value of match
Note: There are no groupings in the regex, so there are no p0, p1, ... parameters positioned between the match and offset parameters.
How do I call an expression within expression? {{ $json.data.replace(/\[\/CODE\]/g, (match,offset) => offset > $json.data.indexOf('[/CODE]') ? '{{ $json.link }}Link5\n[/CODE]' : match) }}