[英]JavaScript: Extract part of string between two optional characters with Regex
[英]Javascript regex - split string by string between two characters
說我有這樣的字符串
鞋子{蛋糕}跟我結婚{fiona:banana | cheese}然后{i want}
我想要一個像這樣的數組
[“鞋子”,“嫁給我”,“然后是”]
甚至
[“鞋子”,“嫁給我”,“然后是”,“”]
我知道如果我只有一個字符,例如
鞋子{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},然后{i want”,““]
.*
將占用盡可能多的空間(“貪婪重復”)。 .*?
將花費盡可能少的時間:
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 ", ""]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.