RegEx to extract all matches from string using RegExp.exec

前端 未结 17 1474
-上瘾入骨i
-上瘾入骨i 2020-11-22 02:49

I\'m trying to parse the following kind of string:

[key:\"val\" key2:\"val2\"]

where there are arbitrary key:\"val\" pairs inside. I want t

17条回答
  •  离开以前
    2020-11-22 03:26

    Continue calling re.exec(s) in a loop to obtain all the matches:

    var re = /\s*([^[:]+):\"([^"]+)"/g;
    var s = '[description:"aoeu" uuid:"123sth"]';
    var m;
    
    do {
        m = re.exec(s);
        if (m) {
            console.log(m[1], m[2]);
        }
    } while (m);
    

    Try it with this JSFiddle: https://jsfiddle.net/7yS2V/

提交回复
热议问题