如何在JavaScript中正则表达多个文本句子/行?

时间:2017-09-06 14:08:15

标签: javascript regex

如果可能的话,我希望能够从一个正则表达式中复制多个句子

/(.*)<FooBar>/s 

这只匹配一个单词(我认为)我需要的东西匹配单词后跟空格。

这是我尝试的正则表达式,但似乎没有用。我不知道它是否是因为javascript语法或其他原因。 (我是正则表达式的新手)

^.*(EVENTS\:OB\:\sRECORDS\snot|EVENTS\:OB\:\sRECORDS\sthat|EVENTS\:OB\:\sUnprocessed).(?<!0\.0)$
  

未捕获的SyntaxError:意外的令牌^

这些是我想要正则表达式的句子的例子

EVENTS:OB: Records not updated to status
EVENTS:OB: Records that were pending OR to be processed for yester day.  
EVENTS:OB: Unprocessed records older than hour.  
RENAS:OB:BOX: Unprocessed records older than hour.  
RENAS:MOSS Pending  
RENAS:TIGNET Pending  
RENAS:OB:SI: Unprocessed records older than hour.  
RENAS:OB:GC: Unprocessed records older than hour.  
RENAS:RENAS:Count of pending message to process. 
RENAS:OB:10+2_Status: Unprocessed records older than hour.
RENAS:RENAS_10+2:Unprocessed records in RENAS 10+2
RENAS:RENAS ACH:Count of pending message to process to FV.
RENAS:CheckNow:Count of pending message to process to FV
RENAS:OB:GB: Unprocessed records older than hour.
RENAS:RENAS: UN Processed records by MIXX for RENAS INSTRUCTONS 
RENAS:RENAS: UN Processed records by MIXX for RENAS Break Bulk 
RENAS:RENAS: UN Processed records by MIXX for RENAS 
RENAS:OB:DOM:Unprocessed count of records older than hour.
RENAS:OB:DOM:Failed records in the database for previous date.
RENAS:OB:SI:Records that were pending to be processed for the previous day.

我一直在做的是这个

function getLines(text) {
  var lines = [];
  var re = /^.*(EVENTS\:OB\:\sRECORDS\snot|EVENTS\:OB\:\sRECORDS\sthat|EVENTS\:OB\:\sUnprocessed).(?<!0\.0)$/;
  while (m = re.exec(text)) {
    lines.push(m[1]);
  };

  return lines;
}

$(function() {
    $('#printlogs').html(getLines($('textarea').val()).join('<br>'));
});

1 个答案:

答案 0 :(得分:0)

您需要一个负面的预测而不是不支持的lookbehind:

/^.*(?:EVENTS:OB:\s(?:RECORDS\snot|RECORDS\sthat|Unprocessed))(?!.*0\.0$).*$/gim

请参阅regex demo

<强>详情

  • ^ - 字符串开头
  • .* - 除了换行符之外的任何0 +字符,尽可能多
  • (?:EVENTS:OB:\s(?:RECORDS\snot|RECORDS\sthat|Unprocessed)) - 一个EVENTS:OB:字符串后面跟着1个空格和任何替代字符:
    • RECORDS\snot - RECORDS,一个空白,not
    • RECORDS\sthat - RECORDS,一个空白,that
    • Unprocessed - Unprocessed子字符串
  • (?!.*0\.0$) - 行尾不能有0.0
  • .*$ - 除了换行符之外的任何0 +字符,尽可能多,直到行尾。

var s = "EVENTS:OB: Records not updated to status\nEVENTS:OB: Records that were pending OR to be processed for yester day.  \nEVENTS:OB: Unprocessed records older than hour.  \nRENAS:OB:BOX: Unprocessed records older than hour.  \nRENAS:MOSS Pending  \nRENAS:TIGNET Pending  \nRENAS:OB:SI: Unprocessed records older than hour.  \nRENAS:OB:GC: Unprocessed records older than hour.  \nRENAS:RENAS:Count of pending message to process. \nRENAS:OB:10+2_Status: Unprocessed records older than hour.\nRENAS:RENAS_10+2:Unprocessed records in RENAS 10+2\nRENAS:RENAS ACH:Count of pending message to process to FV.\nRENAS:CheckNow:Count of pending message to process to FV\nRENAS:OB:GB: Unprocessed records older than hour.\nRENAS:RENAS: UN Processed records by MIXX for RENAS INSTRUCTONS \nRENAS:RENAS: UN Processed records by MIXX for RENAS Break Bulk \nRENAS:RENAS: UN Processed records by MIXX for RENAS \nRENAS:OB:DOM:Unprocessed count of records older than hour.\nRENAS:OB:DOM:Failed records in the database for previous date.\nRENAS:OB:SI:Records that were pending to be processed for the previous day.";
var re = /^.*(?:EVENTS:OB:\s(?:RECORDS\snot|RECORDS\sthat|Unprocessed))(?!.*0\.0$).*$/gim;
document.body.innerHTML = "<pre>" + JSON.stringify(s.match(re), 0, 4) + "</pre>";