{{1}}
我正在寻找一个正则表达式来捕获由一对方括号表示的一个或多个字符串(在javascript中)。我该如何做到这一点?
备用标题案例包括:'[string A] some text [string B]'和'[string A] and no string b'
感谢
答案 0 :(得分:1)
答案 1 :(得分:1)
您需要在循环中调用exec
以与此正则表达式进行多次匹配:
var re = /\[([^\]]*)/g;
var str = '[string A][string B] the rest of the title';
var m;
var matches = [];
while ((m = re.exec(str)) !== null) {
if (m.index === re.lastIndex)
re.lastIndex++;
matches.push(m[1]);
}
console.log(matches);
//=> ["string A", "string B"]