Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

181
Views
How to convert a PCRE regex pattern with match reset for ECMAScript?

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?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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

about 4 years ago · Juan Pablo Isaza Report

0

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.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!