Wednesday, May 2, 2012

JavaScript capture groups

/\w+-(\d+)-\w+/.exec('foo-36-bar')[1];
//   ^   ^
//   |   |
//   L___L_________ The capture group
//

This JavaScript regex extracts the number 36 from the string.

P.S. The index is 1, because index 0 will yield the entire string.

No comments:

Post a Comment