RegEx to extract all matches from string using RegExp.exec

前端 未结 17 1192
-上瘾入骨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:09

    This is a solution

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

    This is based on lawnsea's answer, but shorter.

    Notice that the `g' flag must be set to move the internal pointer forward across invocations.

提交回复
热议问题