Currently, my goal is to create a JavaScript regex that can interpret instances of patterns like \123
and convert them into their corresponding ASCII values. For example, 65
should be replaced with A
.
If the backslash \
itself needs to be included, it can be escaped as \\
, so \\65
would appear as \65
.
The challenge I'm encountering involves correctly parsing consecutive occurrences of the main pattern.
For instance, \65#\97
translates to A#a
. However, \65\97
only changes to A\97
.
Below is a key section of the code:
// Parse \65 but not \\65
input = input.replace(/(^|[^\\]{1})\\(\d{1,3})/g, function (m0, m1, m2) {
var n = parseInt(m2);
if (n < 256) {
return m1 + String.fromCharCode(n);
} else {
return m0;
}
});
You can explore an example demonstrating this behavior in a JSFiddle here.
I suspect the issue lies within the regex, although I haven't been able to pinpoint it yet.
I'm eagerly awaiting any insights or suggestions on resolving this matter :]