[英]I want to replace only the characters outside “” with a regular expression
I want to get the result with regular expression我想用正则表达式得到结果
var text = "\"1test2test3\"test123test45test\"67test89\"";
text.replaceAll(/\"(.*)\"/g, "boom");
boom
but I want但我想要
var text = "\"1test2test3\"test123test45test\"67test89\"";
text.replaceAll(????, "boom");
"\"1test2test3\"boom123boom45boom\"67test89\"";
You can use您可以使用
.replace(/("[^"]*")|test/g, (x,y) => y ? y : "boom")
Details :详情:
("[^"]*")
- Capturing group 1: a "
, then any zero or more chars other than a "
and then a "
("[^"]*")
- 捕获组 1:一个"
,然后是除"
之外的任何零个或多个字符,然后是"
|
- or - 或者test
- a test
string. test
- 一个test
字符串。 The (x,y) => y? y: "boom"
(x,y) => y? y: "boom"
(x,y) => y? y: "boom"
replacement means that whenever Group 1 ( y
) matches, this group value is returned, else (if test
is found in all other contexts), boom
is returned. (x,y) => y? y: "boom"
替换意味着只要第 1 组 ( y
) 匹配,则返回该组值,否则(如果在所有其他上下文中找到test
),则返回boom
。
See the JavaScript demo:请参阅 JavaScript 演示:
var text = "\"1test2test3\"test123test45test\"67test89\""; console.log(text.replace(/("[^"]*")|test/g, (x,y) => y? y: "boom"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.