Javascript 如何吐出字符串以获得每个具有特殊信号的文本?

Javascript 如何吐出字符串以获得每个具有特殊信号的文本?,javascript,Javascript,我有这样的字符串:Stackoverflow是[全世界所有][人]的有用论坛,我喜欢这种朗姆酒 我希望从上面的字符串中获取文本以获得结果: text1 = "all"; text2 = "people"; text3 = "over the world"; 给你: var str = 'Stackoverflow is a useful Forum for [all][people][over the world] and I like this rum'; var matches = s

我有这样的字符串:
Stackoverflow是[全世界所有][人]的有用论坛,我喜欢这种朗姆酒

我希望从上面的字符串中获取文本以获得结果:

text1 = "all";

text2 = "people";

text3 = "over the world";
给你:

var str = 'Stackoverflow is a useful Forum for [all][people][over the world] and I like this rum';
var matches = str.match(/\[.*?]/g);
console.log(String(matches[0]).replace( /(^.*\[|\].*$)/g, '' ));
console.log(String(matches[1]).replace( /(^.*\[|\].*$)/g, '' ));
console.log(String(matches[2]).replace( /(^.*\[|\].*$)/g, '' ));

您的字符串实际上包含括号吗?@isherwood:是的。这是吐痰的信号。看这里:@isherwood:但是有超过1个括号?如何把它们作为我的例子。你确定你的意思是?