I’m referring to this question. Why can’t capturing groups be part of a conditional expression when specifying a string rather than a function as the replacement?
var strings = ["Chapter_a", "Chapter_1", "Chapter_1a"];
strings.forEach((string) => {
console.log(string.replace(/(Chapter)_(d+)(w*)/, `$1${"$2" ? " $2." : ""}${"$3" ? " $3)" : ""}`));
});
Expected output:
Chapter a)
Chapter 1.
Chapter 1. a)
Actual output:
Chapter . a)
Chapter 1. )
Chapter 1. a)
Why is this not working as expected?