通过换行符拆分字符串并包含空行

my_line = [[this
is
a

test]]

split_file = string.match(my_line, "[^\r\n]+|^[ \t\n]*$")
print(#split_file)

上述代码不起作用,我不知道为什么

string.match(my_line, "[^\r\n]+")

这个可以正常工作,但它只匹配单词,所以我的数组看起来像这样["this", "is", "a", "test"]

而我希望数组如下

["this", "is", "a", "\n", "test"]
点赞
用户12957503
用户12957503

不确定你使用的是哪种语言,所以在 JavaScript 中使用全局 (g) 和多行 (m) 标志 -

st = `this
is
a

test`;
console.log(JSON.stringify(st.match(/[^\r\n]+|^[ \t\n]*/gm))); // ["this","is","a","\n","test"]
2020-02-25 17:44:25