Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

183
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda