Home > other >  How can I use regex to find the following pattern?
How can I use regex to find the following pattern?

Time:11-27

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.

CodePudding user response:

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]))
});
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

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, '')))
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

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.

  • Related