簡體   English   中英

Javascript - 使用正則表達式在字符串中查找多次出現的模式

[英]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]);
}

任何幫助將不勝感激

這樣做: \\{\\{(.*?)\\}\\}

您需要通過\\轉義} ,還需要設置非貪婪運算符?

參見: https : //regex101.com/r/F9xdgx/2

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM