JS Regex match numbers between brackets

Viewed 277

I have the following code, but getting no output. When I run it here it seems to work:

https://regex101.com/r/FVkJfF/1

var strng = '[,][123,1][,][456,2][,][789,3][,][,][,][,][,][,][,][,]';
var rgx = \[(.[0-9,]+)\];  
var outstr = strng.match(rgx).join(',');
Write(outstr);
  

To clarify, I aim to capture all the values between the brackets if they contain a number (including the opening and closing bracket of that group)

Guess I am doing something wrong somewhere?

5 Answers

Try this one instead:

/\[\d+,\d+\]/g

It matches a substring if it contains:

  • an opening bracket
  • an integer
  • a comma
  • another integer
  • a closing bracket

… in that order, without any whitespace.

Also note that a regexp literal starts with /. The way you’ve defined var rgx in your snippet is a SyntaxError

Your regular expression pattern seems to be incorrect. Javascript requires you to properly escape the square brackets when using them in a regular expression. This should be your regex pattern:

const rgx = /\[(.[0-9,]+)\]/gm;

This can be further simplified with

const rgx = /\[(.[\d,\d]+)\]/gm;

However, you won't achieve what you're looking for with the join method, because match does not return an array but just the first match, so you can't join a single element. Take a look at the matchAll method.

You need to change the REGEX

\[(?:\d+,\d+)+\]

enter image description here

Use a simple

/\[(\d[\d,]*)]/g

See regex proof.

EXPLANATION

--------------------------------------------------------------------------------
  \[                       '['
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    \d                       digits (0-9)
--------------------------------------------------------------------------------
    [\d,]*                   any character of: digits (0-9), ',' (0
                             or more times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
  )                        end of \1
--------------------------------------------------------------------------------
  ]                        ']'

const regex = /\[(\d[\d,]*)]/g;
const str = `[,][123,1][,][456,2][,][789,3][,][,][,][,][,][,][,][,]`;
let m;

while ((m = regex.exec(str)) !== null) {
   console.log(m[0]);
}

To match all numbers between square brackets with at least a single comma but no consecutive comma's like 1,,2:

\[\d+(?:,\d+)+]

Explanation

  • \[ Match [
  • \d+ Match 1+ digits
  • (?:,\d+)+ Repeat 1+ times matching , and 1+ digits
  • ] Match ]

Regex demo

const regex = /\[\d+(?:,\d+)+]/g;
[
  "[,][123,1][,][456,2][,][789,3][,][,][,][,][,][,][,][,]",
  "[123,1,2,34]",
  "[123,1,2]",
  "[123]",
  "[]",
  "[1,,2]",
  "[,1,2]"
].forEach(s => Array.from(s.matchAll(regex), m => {
  if (m) {
    console.log(m[0]);
  }
}));

Related