如何使用 lookbehind 和 lookahead 零长度断言提取括号内的字符串?

How to extract the strings within brackets using lookbehind and lookahead zero-length assertions?

我想从 "string[0][inner_string]"

中提取 0inner_string

我的方法使用回顾和超前零长度断言。

这是正则表达式:

              +--- Look ahead
              |
              v
/(?<=\[)(.*?)(?=\])/g
  ^
  |
  +--- Look behind

var str = "string[0][inner_string]";
console.log(str.match(/(?<=\[)(.*?)(?=\])/g));

但是,我遇到了一个错误。

是否可以使用环顾四周?

引用自@ctwheels:"Lookbehinds have little support in JavaScript (see the current stage of the TC39 proposal). At the time of writing this only Chrome (starting with version 62) and Moddable (after Jan 17, 2018) support lookbehinds in JavaScript."

正则表达式\[([^\]]+)\]

str = 'string[0][inner_string]'
re = /\[([^\]]+)\]/g

while(match = re.exec(str)) {
  console.log(match[1])
}