I have this code to replace all opening and closing square brackets which has a matching variable inside:
for (var j = 0; j <= temp.length; j++) {
var re = new RegExp("["+j+"]", 'g');
imgData = imgData.replace(re, temp[j]);
}
The line var re = new RegExp("["+j+"]", 'g'); doesn’t work because I assume the brackets aren’t being escaped. Does anyone know how I would escape them, but still be able to have a variable in the pattern? Thanks! 🙂
You should escape it with backslashes: