第29题 用一个正则提取字符串中所有""里内容

     // 如果只是简单的没有循环遍历的话,就只能拿到一个:
     function collectGroup (str) {
      let regExp = /"([^"]*)"/g;
      let match = regExp.exec(str); // [""foo"", "foo"]
      return match[1]; // "foo"
    }
    var str = `"foo" and "bar" and "baz"`
    console.log(collectGroup(str)) // "foo"
    // 第一种方案:使用while循环遍历
     function collectGroup (str) {
      let regExp = /"([^"]*)"/g;
      const matches = [];
      while (true) {
        let match = regExp.exec(str)
        if (match === null) break;
        matches.push(match[1])
      }
      return matches
    }
    var str = `"foo" and "bar" and "baz"`
    console.log(collectGroup(str))
    // 第二种方案:使用ES10的matchAll()
    function collectGroup (str) {
      let regExp = /"([^"]*)"/g;
      const matches = []
      for (const match of str.matchAll(regExp)) {
        matches.push(match[1])
      }
      return matches
    }
    var str = `"foo" and "bar" and "baz"`
    console.log(collectGroup(str))
Last Updated:
Contributors: leeguooooo