Udo G Udo G - 3 months ago 18
Javascript Question

regex to extract array indices

I'm still having a hard time to understand regex... :-/

Given strings (JavaScript-like expressions) like these...


  • foo[0]

  • foo[4][2]

  • foo[4][2][234523][3]



...I'm trying to deconstruct the indices in regex, so that I have


  • the name of the variable:
    foo

  • the single indices: fox example
    4
    ,
    2
    ,
    234523
    and
    3
    in the last example



while not accepting invalid syntax like


  • foo[23]bar[55]

  • foo[123]bar

  • [123]bla

  • foo[urrrr]



It would be nice to also ignore whitespace like
foo [13]
or
foo[ 123 ]
but that's not important.

Is that possible with regex?

I was able to extract the brackets with
var matches = s.match(/\[([0-9]?)\]/g);
but that includes the brackets in the result, is missing the variable name (could get around that) and also does not respect the edge cases as described above.

Answer

You'll have to use loops to extract multiple matches. Here's one way:

function run(string) {
  var match;
  if(match = string.match(/^([^[]+)\s*(\[\s*(\d+)\s*\]\s*)+\s*$/)) {
    var variable = match[1], indices = [];
    var re = /\[\s*(\d+)\s*\]/g;
    while(match = re.exec(string)) {
      indices.push(+match[1]);
    }
    return { variable: variable, indices: indices };
  } else {
    return null;
  }
}

var strings = [
  "foo[0]",
  "foo[4][2]",
  "foo[4][2][234523][3]",
  "foo [13]",
  "foo[ 123 ]",
  "foo[1] [2]",
  "foo$;bar%[1]",
  // The following are invalid
  "foo[23]bar[55]",
  "foo[123]bar",
  "[123]bla",
  "foo[urrrr]",
];

// Demo
strings.forEach(function(string) {
  document.write("<pre>" + JSON.stringify(run(string), null, 4) + "</pre>");
});