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"
.
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"
.
- 1 Why the downvote? The question is ok, there's a clear explanation of the problem and a sample code of what's been tried.. – Ben Commented Aug 26, 2015 at 11:22
4 Answers
Reset to default 5Your 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, "${");
Just problem with escape characters.
use \
before [
.
var str = "it is [[test example [[testing";
var res = str.replace(/\[\[/g, "${");
If you don't want to use regex
var res = str.split('[[').join('${');
Sample Here:
var str = "it is [[test example [[testing";
var res = str.split('[[').join('${');
document.write(res);