-3

I have regexp:

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

And my string:

[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. 

I need to retrive text of each person. But I my problem is that text could contain newlines \n and my regexp retrives text only to the end of the line. Please look at the example here https://regex101.com/r/t7zV1U/1

Dmitro
  • 1,489
  • 4
  • 22
  • 40
  • Possible duplicate of [Regular Expression to get a string between two strings in Javascript](https://stackoverflow.com/questions/5642315/regular-expression-to-get-a-string-between-two-strings-in-javascript) – Yassine Badache Dec 20 '17 at 14:02
  • _my problem is that text could contain newlines \n and my regexp retrives text only to the end of the line._ Just set multiline flag (`m`) – hindmost Dec 20 '17 at 14:03
  • @hindmost, I use flags gm, I edited post. – Dmitro Dec 20 '17 at 14:05
  • 1
    [`\[(.*?)\] \[(.*?)\] \[(.*?)\] ([^\[]*)`](https://regex101.com/r/t7zV1U/2) ? – Gurmanjot Singh Dec 20 '17 at 14:09
  • @Gurman, cool it's work. thank you – Dmitro Dec 20 '17 at 14:12
  • @Dmitro Gurman's answer is fantastic but it will fail if `[` is found in any sentences. You need to use a tempered greedy token to ensure it stops matching when it finds `[` at the start of the string (as I've done in my answer) – ctwheels Dec 20 '17 at 14:42

2 Answers2

1

Code

See regex in use here: Switch to PCRE flavour to see actual matches live in string.

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

Usage

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}`);
    });
}

Explanation

First to note is that I changed all of your \[(.*?)\] to \[([^\]]*)\]. This is because \[([^\]]*)\] actually performs better than the lazy quantifier in this case ([^\]]* means match anything that is not ]).

  • ((?:(?!^\[)[\s\S])*) Capture the following into capture group 4
    • (?:(?!^\[)[\s\S])* Match the following any number of times (this is a tempered greedy token)
      • (?!^\[) Negative lookahead ensuring what follows doesn't match
        • ^ Assert position at the start of the line
        • \[ Match the left square bracket [ literally
      • [\s\S] Match any character (includes newline characters)
ctwheels
  • 21,901
  • 9
  • 42
  • 77
0

It should be the Group 1 of this RegExp: \[(.*?)\] \[(.*?)\] \[(.*?)\]/gm

See: https://regex101.com/r/tC6mDp/1

You just need to match the brackets:

  • Group 1 will be the name
  • Group 2 the date
  • Group 3 the topic?
hardkoded
  • 18,915
  • 3
  • 52
  • 64