In a string, I need to replace all occurences of [A], "A", 'A' and _A_ with [X], "X", 'X' and _X_. I tried this answer but it's giving a weird result (code below). I use new RegExp because it allows interpolating a variable -- A and X are just an example.
// CAPTURE GROUPS: |------1-----|2|-----3------|
var regexp = new RegExp('(\'|\"|\_|\[)(A)(\'|\"|\_|\])', 'g')
var string = ' [A] _A_ "A" \'A\' A BAB "B" '
string.replace(regex, '$1X$3')
// ORIGINAL: [A] _A_ "A" 'A' A BAB "B"
// EXPECTED: [X] _X_ "X" 'X' A BAB "B"
// ACTUAL: [X] XXX XXX XXX X BXB XBX