换行后如何使用正则表达式javascript获取结果

时间:2018-08-02 06:11:47

标签: javascript regex

我有一个日志文件和以下数据出现,但我需要最后的数据并希望在我们的系统中进行注释。

2018-07-25 10:38:33.711503-0700 0x287f4  0x0    119    0    checked: [version:default] Server task 0x1013e3620: checked sentence: 'call to test 1', {
"is_device" = 0;
"is_call" = 0;
"is_system" = 0;
"is_time_out" = 0;
out = "check server data";

}

现在,我正在使用正则表达式javascript,但直到

我才获取数据
2018-07-25 10:38:33.711503-0700 0x287f4  0x0    119    0    checked: [version:default] Server task 0x1013e3620: checked sentence: 'call to test 1', {

它并不能提供所有输出,因此我在正则表达式中尝试了\ n。,\ r。,[\ s \ S] *,但没有任何效果。

这是我的代码,

var CheckedRegex = new RegExp(/.*(checked sentence.*)/)
var CheckingArray = new Array()
var LastChecked = ""
var matched = 0

function scan(doc) {
  var found = ""
  if(found = doc.text.match(CheckedRegex)) {
    CheckingArray.push(found)
    LastChecked = found
    matched++
  }
}

function comment() {
  if(matched) {
    return LastChecked[0]
    }

输出:

2018-07-25 10:38:33.711503-0700 0x287f4  0x0    119    0    checked: [version:default] Server task 0x1013e3620: checked sentence: 'call to test 1', {

预期:

    2018-07-25 10:38:33.711503-0700 0x287f4  0x0    119    0    checked: [version:default] Server task 0x1013e3620: checked sentence: 'call to test 1', {
    "is_device" = 0;
    "is_call" = 0;
    "is_system" = 0;
    "is_time_out" = 0;
    voice = "check server data";
} 

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

一个选项是

^.+checked sentence[^}]+}

匹配一行的开头,然后是字符(不包括换行符),然后是checked sentence,然后是字符包括换行符,直到达到}

const re = /^.+checked sentence[^}]+}/m;
const text = `text
more text
    2018-07-25 10:38:33.711503-0700 0x287f4  0x0    119    0    checked: [version:default] Server task 0x1013e3620: checked sentence: 'call to test 1', {
    "is_device" = 0;
    "is_call" = 0;
    "is_system" = 0;
    "is_time_out" = 0;
    voice = "check server data";
}`;
console.log(text.match(re));

https://regex101.com/r/We6iQL/1

此外,也不需要new RegExp-如果您可以键入正则表达式文字,则只需执行该操作即可,例如

var CheckedRegex = /^.+checked sentence[^}]+}/m

答案 1 :(得分:0)

您的正则表达式.*(checked sentence.*)仅与第一行匹配,因为dot与换行符不匹配。

您可以为示例匹配日期,例如模式,然后使用[\S\S]*?匹配任何字符,并使用?使其不贪婪。

然后匹配checked sentence,然后使用否定字符类}匹配[^}]+,然后匹配}

^\d{4}-\d{2}-\d{2}[\s\S]*?checked sentence[^}]+}

var CheckedRegex = /^\d{4}-\d{2}-\d{2}[\s\S]*?checked sentence[^}]+}/gm;

var CheckedRegex = /^\d{4}-\d{2}-\d{2}[\s\S]*?checked sentence[^}]+}/gm;
var string = `test

2018-07-25 10:38:33.711503-0700 0x287f4  0x0    119    0    checked: [version:default] Server task 0x1013e3620: checked sentence: 'call to test 1', {
    "is_device" = 0;
    "is_call" = 0;
    "is_system" = 0;
    "is_time_out" = 0;
    out = "check server data";
}
test

2018-07-25 10:38:33.711503-0700 0x287f4  0x0    119    0    checked: [version:default] Server task 0x1013e3620: checked sentence: 'call to test 1', {
        "is_device" = 0;
        "is_call" = 0;
        "is_system" = 0;
        "is_time_out" = 0;
        voice = "check server data";
    } 


2018-07-25 10:38:33.711503-0700 0x287f4  0x0    119    0    checked: [version:default] Server task 0x1013e3620: checked sentence: 'call to test 1', {
        "is_device" = 0;
        "is_call" = 0;
        "is_system" = 0;
        "is_time_out" = 0;
        voice = "check server data";
    }

2018-07-25 10:38:33.711503-0700 0x287f4 0x0 119 0 checked: [version:default] Server task 0x1013e3620: checked sentence: 'call to test 1', { "is_device" = 0; "is_call" = 0; "is_system" = 0; "is_time_out" = 0; voice = "check server data"; }


2018-07-25 10:38:33.711503-0700 0x287f4 0x0 119 0 checked: [version:default] Server task 0x1013e3620: checked sentence: 'call to test 1', { "is_device" = 0; "is_call" = 0; "is_system" = 0; "is_time_out" = 0; voice = "check server data"; }`;
console.log(string.match(CheckedRegex));