I want to use the regex capture group to translate UTF-16 values in an ID.
var x = "ELEMENTT_x5F_GS1_x5F_MARKETING_x5F_CLAIM__x3B__INSTANCE_x3D_3__x3B__LOCALE_x3D_1_"
var regex = /_(x..)_/g;
var replacedID = x.replaceAll(regex, String.fromCodePoint("0" + "$1"));
however this does not appear to fill in the captured value into the string the same way it normally works.
i have tried any which way and it does not seem to recognize the “$1” as replace capture group.
0
The issue is that the replacement is made after the String.fromCodePoint
call has completed.
Here’s an example that might help explain:
console.log("abc".replaceAll(/a(b)c/g, "x$1x".toUpperCase()))
Here, x$1x
is converted to uppercase (-> X$1X
) and then passed to the replaceAll, returning XbX
(rather than potentially expected XBX
).
The solution is detailed in this MDN reference
You can specify a function as the second parameter. In this case, the function will be invoked after the match has been performed. The function’s result (return value) will be used as the replacement string
Converting the above sample becomes:
console.log("abc".replaceAll(/a(b)c/g, (_, p1) => `x${p1}x`.toUpperCase()))
(where p1
is the equivalent of $1
)
thus applying the toUpperCase after the replacement text has been applied
This can then be applied in your case as:
let x = "ELEMENTT_x5F_GS1_x5F_MARKETING_x5F_CLAIM__x3B__INSTANCE_x3D_3__x3B__LOCALE_x3D_1_"
let regex = /_(x..)_/g;
var replacedID = x.replaceAll(regex, (m, p1) => String.fromCodePoint("0" + p1));
console.log(replacedID);
Note: it’s unclear if the extra _
around the UTF-16 code should be included or not, you might need this regex:
let regex = /(?<=_)(x..)(?=_)/g;
or just change the replace string to include then 2x _