0
我有分裂字符串与模式的问题。我知道我可以使用String.prototype.split
来拆分字符串。但是,如果我使用它,我会失去分隔符。如何用字符串分割字符串而不丢失任何文本?
例如
const text = '{user} foo bar {time} test test';
const arr = text.split(/\{[a-z]*\}/)
// arr = ["", " foo bar ", " test test"]
我期望是["{user}", " foo bar ", "{time}", " test test"]
是否有可能实现与split
?
使用'text.split(/(\ {[AZ] *})/)过滤器(布尔)' –
你不知道。需要任何复杂的RegExp。试试这个'text.split(“”);' – Krusader
@Krusader它不会工作,因为''foo bar“'和''test test”' –