admin管理员组文章数量:1025278
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);
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);
本文标签: regexhow to replace 3939 with 3939 in javascriptStack Overflow
版权声明:本文标题:regex - how to replace '[[' with '${' in javascript - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745615397a2159260.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论