Skip to content

JS Regex match numbers between brackets

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?

Answer

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