如何从文本中检索第二个值

How to retrieve second value from a text?

本文关键字:检索 第二个 文本      更新时间:2023-09-26

如何获得文本的第二个值?例:ab - 10:12 34。我希望函数显示1234而不是10。

function myfunction(){
var txtNIM = document.getElementsByName("txtNIM")[0].value;
var somArr = txtNIM.split(String.fromCharCode(10));
[...]
} 
<textarea name="txtNIM" cols="70" rows="9">
ABC-10: 1234
CBA-20: 4321
</textarea>

有几种不同的方法来实现您想要的。如果您不想使用正则表达式,并且目标是相对较新的浏览器,这应该可以正常工作:

var getSecondValue = function(row) {
    var allRows = document.querySelector("textarea[name='txtNIM']").innerHTML.trim().split("'n");
    var str = allRows[row];
    return str.slice(str.indexOf(':')+1).trim();
}

getSecondValue(1); //4321