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

280
Visualizações
How can I use regex to find the following pattern?

I have an array of strings like so:

["Author Name, (p. 123). (2019). Company.", "Author Name, (p. 321). (2021). Company."]

How can I return the page numbers that start with and contain the pattern (p.?

So far I have tried /\(([^)\)]+)\)/ however it returns everything with parentheses. I only want the page numbers with parentheses.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can match the p. before the capture group and capture the numbers. You don't have to escape the parenthesis in the character class, so you can remove \) and leave just )

\(p\.\s*([^)]+)\)

See a regex demo.

const regex = /\(p\.\s*([^)]+)\)/g;
[
  "Author Name, (p. 123). (2019). Company.",
  "Author Name, (p. 321). (2021). Company."
].forEach(s => {
  Array.from(s.matchAll(regex), m => console.log(m[1]))
});

about 4 years ago · Juan Pablo Isaza Relatório

0

I'd capture digits (you need (p.s.)?), case-insensitive (what about (P.12)?):

/\(p\.\s*(\d+)\)/ig

Code

const regex = /\(p\.\s*(\d+)\)/gi
const string = "Author Name, (p. 123). (2019). Company."
console.log(string.match(regex).map(x => x.replace(/\D+/g, '')))

EXPLANATION

NODE                     EXPLANATION
--------------------------------------------------------------------------------
  \(                       '('
--------------------------------------------------------------------------------
  p                        'p'
--------------------------------------------------------------------------------
  \.                       '.'
--------------------------------------------------------------------------------
  \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                           more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    \d+                      digits (0-9) (1 or more times (matching
                             the most amount possible))
--------------------------------------------------------------------------------
  )                        end of \1
--------------------------------------------------------------------------------
  \)                       ')'

See regex proof.

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