繁体   English   中英

查找行中的数字。 使用Javascript

[英]Find the number in line. Javascript

如何找到行号中的数字可能不是开头。 例如:“ d:\\ \\ 1.jpg”

谢谢。

您可以使用regexp

var match = "testing 123".match(/\d/);
if (match) {
    alert(match.index); // alerts 8, the index of "1" in the string
}

它使用String#match ,使用“ digit”类( \\d )传递文字正则表达式。

和/或您可以获取从找到的第一个数字开始的所有连续数字:

var match = "testing 123".match(/\d+/);
if (match) {
    alert(match.index); // alerts 8, the index of "1" in the string
    alert(match[0]);    // alerts "123"
}

这些链接指向Mozilla文档,因为它相当不错,但是它们不是Mozilla特定的功能。

您将正则表达式与RegExp对象一起使用:

var myRegEx = /\d+/; // RegEx to find one or more digits

var myMatch = myRegEx.exec("d:\\1.jpg")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM