JavaScript拆分了一个在字符串中有新换行符的字符串

时间:2016-12-19 21:13:12

标签: javascript regex string split

我有一个非常长的日志文件,每个条目都以新行开头。但有些参赛作品中有新的换行符。所以我使用这个代码拆分我的日志文件,然后我对它运行不同的正则表达式规则,一切正常: var str = data.split('\n');

一旦我有一些更复杂的文本,其中包括字符串中的换行符。我的代码坏了。以下是日志文件的示例。第一行是正常的,第二行结束于(此处结束)。

3708 07:11:59 INFO  (username): SAVE: master:/url_path, language: en, version: 1, id: {1846518641516}    
908 07:11:40 INFO  (username): SAVE: master:/url_path, language: en, version: 1, id: {148815184185}, ** [Content]: new: Please note the following when using this app:

▪  Some text
▪  Some text
▪  Some text
▪  Some more and more text., old: Please note the following when using this app:

▪  Some text
▪  Some text
▪  Some text
▪  Some text
▪  Some text
▪  Some text
ends here

希望我的问题很明确。 我应该如何重构我的var str = data.split('\n');以使其适用于这两种条目?

感谢您的帮助

1 个答案:

答案 0 :(得分:3)

你需要在\n分割,后跟一串数字,一个空格和一个类似时间的字符串:

s.split(/\n(?=\d+ \d{2}:\d{2}:\d{2}\b)/)

请参阅regex demo

<强>详情:

  • \n - 换行后跟......
  • (?=\d+ \d{2}:\d{2}:\d{2}\b) - (一个积极的前瞻,只要求右边的字符串符合模式,否则会发生失败)
    • \d+ - 一位或多位
    • - 空格
    • \d{2}:\d{2}:\d{2} - 2个数字,:两次,再次2个diigts
    • \b - 尾随字边界

var s = "3708 07:11:59 INFO  (username): SAVE: master:/url_path, language: en, version: 1, id: {1846518641516}    \r\n908 07:11:40 INFO  (username): SAVE: master:/url_path, language: en, version: 1, id: {148815184185}, ** [Content]: new: Please note the following when using this app:\r\n\r\n▪  Some text\r\n▪  Some text\r\n▪  Some text\r\n▪  Some more and more text., old: Please note the following when using this app:\r\n\r\n▪  Some text\r\n▪  Some text\r\n▪  Some text\r\n▪  Some text\r\n▪  Some text\r\n▪  Some text\r\nends here";
var res = s.split(/\n(?=\d+ \d{2}:\d{2}:\d{2}\b)/);
console.log(res);