JavaScript – Capture repeated group

Using JavaScript (or VBScript) Is it possible to separately capture from %n:32+5000 the following groups?:

  • Capture group 1: %n
  • Capture group 2: :32
  • Capture group 3: +5000

I tried a search through this forum about capturing repeated groups, but the examples given were either in a different language like .NET or the asker wasn’t asking specifically how to do what I need.

The best attempt I’ve made thus far is (%n)(([:+]d+){0,2}) with global turned on. Also, I am using https://regex101.com/r/qBylQX/1 to help me visualize what’s happening; but, so far I haven’t cracked it.

Notes:

  • only one instance of %n is allowed per
  • only one appearance of :d+ is allowed per match
  • only one appearance of +d+ is allowed per match.
  • the pattern can appear anywhere in a string.

Answer

Use

(%n)(?:(:d+)(+d+)?)?

See regex proof.

EXPLANATION

--------------------------------------------------------------------------------
  (                        group and capture to 1:
--------------------------------------------------------------------------------
    %n                       '%n'
--------------------------------------------------------------------------------
  )                        end of 1
--------------------------------------------------------------------------------
  (?:                      group, but do not capture (optional
                           (matching the most amount possible)):
--------------------------------------------------------------------------------
    (                        group and capture to 2:
--------------------------------------------------------------------------------
      :                        ':'
--------------------------------------------------------------------------------
      d+                      digits (0-9) (1 or more times
                               (matching the most amount possible))
--------------------------------------------------------------------------------
    )                        end of 2
--------------------------------------------------------------------------------
    (                        group and capture to 3 (optional
                             (matching the most amount possible)):
--------------------------------------------------------------------------------
      +                       '+'
--------------------------------------------------------------------------------
      d+                      digits (0-9) (1 or more times
                               (matching the most amount possible))
--------------------------------------------------------------------------------
    )?                       end of 3 (NOTE: because you are using a
                             quantifier on this capture, only the
                             LAST repetition of the captured pattern
                             will be stored in 3)
--------------------------------------------------------------------------------
  )?                       end of grouping