JS中的正则表达式以获取分隔字符之间的文本



嗨,我一直在搜索并尝试在 JS 中使用正则表达式对分隔字符之间的文本进行简单的数据提取,但不包括分隔字符。我以为这很简单,但我就是无法理解。

Text = "This is a [sample] text to be [extracted] by Regex function."
Result I am looking for     = sample, extracted
Result I am not looking for = [sample], [extracted]

有人可以帮忙吗?

使用积极的回顾和积极的前瞻,例如:/(?<=[).*?(?=])/

例如

let text = "This is a [sample] text to be [extracted] by Regex function."
let regex = /(?<=[).*?(?=])/g
console.log(text.match(regex))