-1

with the following code can get the 1 in the string.

var match = /[0-9]+/.exec('[1][2]');
console.log(match);

How do i get the 2 and not the 1 ?

Vinicius Luiz
  • 49
  • 1
  • 3
  • You need to use a while loop, see MDN for an example: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec Note that if your goal is to perform a replacement, you don't need to use the exec method, use String.prototype.replace with the g parameter. – Casimir et Hippolyte Apr 19 '16 at 12:24
  • Use `var match = '[1][2]'.match(/[0-9]+/g).pop();` – Wiktor Stribiżew Apr 19 '16 at 12:32
  • the final code is ugly but it works. Thank you! `item.attr( { name: name.replace(name.match(/[0-9]+/g).pop(), clone.attr('data-ordem')) } );` – Vinicius Luiz Apr 19 '16 at 12:46

1 Answers1

0

Try with this regex

console.log(/(\[[0-9]\])+/.exec('[1][2]'));

I think it's only a matter of escaping the square brackets

Pietro
  • 988
  • 10
  • 19
  • You can't repeat a capture group because at each iteration, the previous capture is overwritten with the next. – Casimir et Hippolyte Apr 19 '16 at 12:29
  • Yes, but he asked to get the `2`, I assumed it as "get the latest one", the related answer (marked as duplicate) has a better answer anyway :) – Pietro Apr 19 '16 at 12:30