![](/img/trans.png)
[英]JavaScript RegEx to obtain one or more occurrences of a specific pattern in a string
[英]Javascript - Use regex to find multiple occurrences of a pattern in a string
我有一个这种格式的字符串
"{{abc}}, {{def}}, some text {{ghi}}, some other text {{jkl}}"
我想用一些基于什么的字符串来替换每个{{...}}
...
(来自 json 而不是问题)并坚持使用什么正则表达式模式。 我能来的最接近的是/{{(.*)}}/gi
,但这会返回整个字符串,因为它的末尾有{{
和}}
。 这是我试过的代码
let str = "{{abc}}, {{def}}, some text {{ghi}}, some other text {{jkl}}";
let regex = /{{(.*)}}/gi;
let result;
let indices = [];
let results = [];
while ((result = regex.exec(str))) {
indices.push(result.index);
results.push(result[0]);
}
任何帮助将不胜感激
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.