0

I want to split the following lines into an array in 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

I can do this with:

var arr = data.split(/\r?\n/);

Which gets me that:

[
    "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"
]

So far so good, but the problem is, that I want not 6 items in that array, I want just 4 something like this:

[
    "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"
]

I played around some time with the js .match() and .split() functions, but couldn't figure it out.

Here is as jsbin: http://jsbin.com/icufef/1/edit

4

3 に答える 3

0

\r?\n(Jan|Feb|March|Apri...|Dec)改行に続いて月の短縮名を検索する必要があるため、分割引数のようなものになります。データがこれらの月の名前をどのように提供するかを知る必要があり、「テスト」の代わりに「可能性があります」が来ないことを知っておく必要があります。

編集:ああ、ザビエルは正しいです:それを分割する代わりに、それらのエントリを実際の改行としてマークする必要があります:

data.replace('/^(Jan|Feb...) /', 'BREAKME$1');
data.split('/\r?\nBREAKME');
于 2013-06-02T17:43:24.510 に答える