I have the following string:
[TITLE|prefix=a] [STORENAME|prefix=b|suffix=c] [DYNAMIC|limit=10|random=0|reverse=0]
And I would like to get the value of the prefix of TITLE, which is a.
I have tried it with (?<=TITLE|)(?<=prefix=).*?(?=]|\|)
and that seems to work but that gives me also the prefix of STORENAME (b). So if [TITLE|prefix=a]
will be missing in the string, I'll have the wrong value.
So I need to set the end of the match with ]
that belongs to [TITLE
. Please notice that this string is dynamic. So it could be [TITLE|suffix=x|prefix=y]
as well.
const regex = "[TITLE|prefix=a] [STORENAME|prefix=b|suffix=c] [DYNAMIC|limit=10|random=0|reverse=0]".match(/(?<=TITLE|)(?<=prefix=).*?(?=]|\|)/);
console.log(regex);