Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

284
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda