Home > database >  How to convert a PCRE regex pattern with match reset for ECMAScript?
How to convert a PCRE regex pattern with match reset for ECMAScript?

Time:03-29

Suppose I have the following pattern:

|              |                 |      |      |         |
|            |           |      |      |         |
|              |     |      |      |         |

I am interested in a regex expression that can match anything between the second and third | characters, as shown below.

enter image description here

I can do this using the following pattern ^\|\s \|\K\s (?=\|) (regex demo here) with the PCRE flavor.

However, my requirement is to find a pattern supported by the ECMAScript flavor. The use of the match reset (i.e., \K) does not work in this case. Do you have any ideas on how to obtain something similar that works in ECMAScript?

CodePudding user response:

As indicated in the comments this can be much easier solver by splitting the string on |.

// The string with 'a', 'b', and 'c' added for clarity.
const string = "|              |        a         |      |      |         |\n|            |     b      |      |      |         |\n|              |   c  |      |      |         |";

// Print the string.
console.log(string);

// Split and print the regions between the 2nd and 3rd `|`.
string.split('\n').forEach(line => {
    console.log(line.split('|', 3)[2])
});

CodePudding user response:

While splitting is indeed easier, I still wanted to see if I could do it with regex. You can use lookahead to match the pattern from the beginning of the line, and within the lookahead, capture the contents of what you're looking for in a capture group.

Example: https://regex101.com/r/v0GnAN/1

(?=^\|[^|]*\|([^|]*?)\|)

This is a lookahead group that's looking for anything between two | characters, then matching the same but within a capture group. This is slightly different from the \K approach in that your results are now in group 1 instead of group 0.

  • Related