你如何得到两个标记之间的字符串,即使它里面的字符串在节点中有多个

How do you get the the string between two markers even if there is multiple for the string inside it in nodes

我试图制作一个接受字符串并获取其中所有内容的节点程序:

var str = "Hello {world}!";

console.log(getBracketSubstrings(str)); // => ['world']

它有效,但当我这样做时:

var str = "Hello {world{!}}";
console.log(getBracketSubstrings(str)); // => ['world{!']

它returns ['world{!}'],当我想要它return:

['world{!}']

是否可以对节点中的字符串执行此操作?

您可以使用带有捕获组的模式,从 { 开始匹配,然后使用 [^}]* 匹配除结束卷曲之外的任何字符,直到遇到 }

{([^}]*)}

看到一个regex demo

const getBracketSubstrings = s => Array.from(s.matchAll(/{([^}]*)}/g), x => x[1]);
console.log(getBracketSubstrings("Hello {world}!"));
console.log(getBracketSubstrings("Hello {world{!}}"));