如何在两个以上的另一个字符串之间找到字符串?

时间:2017-12-20 14:00:18

标签: javascript regex

我有正则表达式:

let re = /\[(.*?)\] \[(.*?)\] \[(.*?)\] (.*)/gm;
let regex = new RegExp(re);

我的字符串:

[Max] [2017-12-12 15:59 (UTC +02:00)] [Technical issues] Lorem Ipsum is simply dummy text of the printing and typesetting industry. 
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. 
[Corben Dallas] [2017-12-20 12:48 (UTC +02:00)] [Technical issues] Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. 
[Max] [2017-12-12 15:59 (UTC +02:00)] [Technical issues] Lorem Ipsum is simply dummy text of the printing and typesetting industry. 
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. 

我需要翻阅每个人的文字。但我的问题是文本可能包含换行符\n而我的正则表达式仅将文本复制到行尾。 请查看此处的示例https://regex101.com/r/t7zV1U/1

2 个答案:

答案 0 :(得分:1)

代码

See regex in use here:切换到PCRE风格以查看实际匹配字符串。

\[([^\]]*)\] \[([^\]]*)\] \[([^\]]*)\] ((?:(?!^\[)[\s\S])*)

用法

var s = `[Max] [2017-12-12 15:59 (UTC +02:00)] [Technical issues] Lorem Ipsum is simply dummy text of the printing and typesetting industry. 
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. 
[Corben Dallas] [2017-12-20 12:48 (UTC +02:00)] [Technical issues] Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. 
[Max] [2017-12-12 15:59 (UTC +02:00)] [Technical issues] Lorem Ipsum is simply dummy text of the printing and typesetting industry. 
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. `

var r = /\[([^\]]*)\] \[([^\]]*)\] \[([^\]]*)\] ((?:(?!^\[)[\s\S])*)/gm
let m;

while ((m = r.exec(s)) !== null) {
    // This is necessary to avoid infinite loops with zero-width matches
    if (m.index === r.lastIndex) {
        r.lastIndex++;
    }
    
    // The result can be accessed through the `m`-variable.
    m.forEach((match, groupIndex) => {
        console.log(`Found match, group ${groupIndex}: ${match}`);
    });
}

说明

首先要注意的是,我已将您的所有\[(.*?)\]更改为\[([^\]]*)\]。这是因为在这种情况下,\[([^\]]*)\]实际上比延迟量词更好([^\]]*表示匹配任何非])。

  • ((?:(?!^\[)[\s\S])*)将以下内容捕获到捕获组4中
    • (?:(?!^\[)[\s\S])*匹配以下任意次数(这是tempered greedy token
      • (?!^\[)否定前瞻确保后续内容不匹配
        • ^在行首处断言位置
        • \[按字面意思
        • 匹配左方括号[
      • [\s\S]匹配任何字符(包括换行符)

答案 1 :(得分:0)

应该是此RegExp的第1组\[(.*?)\] \[(.*?)\] \[(.*?)\]/gm

请参阅:https://regex101.com/r/tC6mDp/1

您只需要匹配括号:

  • 第1组将是名称
  • 第2组日期
  • 第3组主题?