我想下面的行拆分为一个数组中的Javascript:正则表达式分割线在JavaScript
Jun 02 16:45:04 [steveh] [info] test1
Jun 02 16:45:12 [steveh] [info] test2
Jun 02 16:45:12 [steveh] [info] test3
test 3.1
test 3.2
Jun 02 16:45:16 [steveh] [info] test4
我能做到这一点有:
var arr = data.split(/\r?\n/);
它得到我说:
[
"Jun 02 16:45:04 [steveh] [info] test1",
"Jun 02 16:45:12 [steveh] [info] test2",
"Jun 02 16:45:12 [steveh] [info] test3",
"test 3.1",
"test 3.2",
"Jun 02 16:45:16 [steveh] [info] test4"
]
到目前为止好,但问题是,我不想在该数组中的6个项目,我只想要4这样的事情:
[
"Jun 02 16:45:04 [steveh] [info] test1",
"Jun 02 16:45:12 [steveh] [info] test2",
"Jun 02 16:45:12 [steveh] [info] test3
test 3.1
test 3.2",
"Jun 02 16:45:16 [steveh] [info] test4"
]
我打周围的一些时间与JS .match()
和.split()
功能,但无法弄清楚。
这里是jsbin:http://jsbin.com/icufef/1/edit
你从哪里得到这些数据? – Blender
实际上来自一个给服务器记录回来的web服务。我想过滤该日志中的JavaScript,因此我需要一个数组。但是为了测试,你可以像处理多行字符串一样处理它。 – MrAsk
如果你知道你想得到的确切格式,你可以尝试一个正则表达式来匹配那个字符串,而不是分割。 [**在线演示**](http://regex101.com/r/mH7qW0) – HamZa