我们如何在Javascript中获得字符串的第二个数值

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

本文关键字:字符串 第二个 Javascript 我们      更新时间:2023-09-26

我想获得字符串的第二个数值,我想知道如何做到这一点。

我遇到的问题是,我需要从以下元素的名称属性中获得值5:

<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]">

我曾想过使用regex,但它太复杂了。我认为得到这个的最好方法是以某种方式拉取数值,得到第二个;但是,我不知道该怎么做。

如果我正确理解这个问题,例如,你想解析名称属性值,那么这种正则表达式应该很好:

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);