说我有一个像这样的字符串
鞋{蛋糕}嫁给我{fiona:banana | cheese}然后{i want}
我想得到一个像这样的数组
["鞋子","嫁给我","然后"]
甚至
["鞋子","嫁给我","然后",""]
我知道如果我有一个char,例如
鞋子{a}嫁给我{b}然后{c}
我能做到
"Shoes {a} marry me {b} and then {c}".split(/\{.\}/)
获得我的结果。
我试过
"Shoes {cakes} marry me {fiona:banana|cheese} and then {i want}".split(/\{(.*)\}/)
但我的结果看起来像
["鞋子","蛋糕}嫁给我{fiona:banana | cheese}然后{我想",""]
答案 0 :(得分:3)
.*
将采取尽可能多的措施(“贪婪的重复”)。 .*?
会尽可能少:
let haystack = "Shoes {cakes} marry me {fiona:banana|cheese} and then {i want}";
let needle = /{.*?}/;
console.log(haystack.split(needle));
// => ["Shoes ", " marry me ", " and then ", ""]