I have a string that looks like this: [TITLE|prefix=a].
[TITLE|prefix=a]
From that string, the text |prefix=a is dynamic. So it could be anything or emp
|prefix=a
I think the solution to your problem would look similar to this:
let str = 'Lorem ipsum [TITLE|prefix=a] dolor [sit] amet [consectetur]'; str = str.replace(/(\[[^\|\]]+)(\|[^\]]*)?\]/g, "$1$2|suffix=z]"); console.log(str);