I tried to replace [[ with ${.
var str = "it is [[test example [[testing";
var res = str.replace(/[[[]/g, "${");
I am getting the result "it is ${${test example ${${testing" but I want the result "it is ${test example ${testing".
Your regex is incorrect.
[[[]
will match one or two [ and replace one [ by ${.
See Demo of incorrect regular expression.
[ is special symbol in Regular Expression. So, to match literal [,
you need to escape [ in regex by preceding it \. Without it [ is treated as character class.
var str = "it is [[test example [[testing";
var res = str.replace(/\[\[/g, "${");
// ^^^^
document.write(res);
you want to escape the [ using \
var res = str.replace(/\[\[/g, "${");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With