我试图取代[[用${.
var str = "it is [[test example [[testing";
var res = str.replace(/[[[]/g, "${");
Run Code Online (Sandbox Code Playgroud)
我得到的结果,"it is ${${test example ${${testing"但我想要的结果"it is ${test example ${testing".
你的正则表达式是不正确的.
[[[]
Run Code Online (Sandbox Code Playgroud)
将匹配一个或两个 [,更换一个[通过${.
请参阅演示错误的正则表达式.
[是正则表达式中的特殊符号.因此,为了匹配文字[,你需要逃跑[中regex被它前面\.没有它[被视为字符类.
var str = "it is [[test example [[testing";
var res = str.replace(/\[\[/g, "${");
// ^^^^
document.write(res);Run Code Online (Sandbox Code Playgroud)