Q:

How do we get the second numerical value of a string in Javascript?

I would like to get the second numerical value of a string and I would like to know how to do this.
The problem I have is that I need to get the value 5 out of the name attributes of the elements below:
<input type="hidden" value="canada" name="ffr[100][ap][5][id]">
<input type="hidden" value="76" name="rre[200][ap][5][opt]">
<input type="hidden" value="f12e" name="hgfp[450][addp][5][name]">
<input type="hidden" value="ap" name="gfgp[500][abp][5][type]">

I thought about using regex, but it's too complicated. I think the best way to get this is to somehow pull the numerical values and get the second one; however, I am not sure how to do this.

A:

If I understand the question correctly, e.g. you want to parse the name attribute values, then this kind of regex should work just fine:

var value = 'abc[500][xyz][5][3784]'; // the value you want to extract the second number from
var number = Number(/\d+\D+(\d+)/.exec(value)[1]);
document.write(number);

