[英]Find the number in line. Javascript
How do I find the number in line number may not be in the beginning. 如何找到行号中的数字可能不是开头。 For example: "d: \\ \\ 1.jpg"
例如:“ d:\\ \\ 1.jpg”
Thank you. 谢谢。
You can use a regexp : 您可以使用regexp :
var match = "testing 123".match(/\d/);
if (match) {
alert(match.index); // alerts 8, the index of "1" in the string
}
That uses String#match , passing in a literal regexp using the "digit" class ( \\d
). 它使用String#match ,使用“ digit”类(
\\d
)传递文字正则表达式。
And/or you can grab all contiguous digits starting with the first digit found: 和/或您可以获取从找到的第一个数字开始的所有连续数字:
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"
}
Those links are to the Mozilla documentation because it's fairly good, but these are not Mozilla-specific features. 这些链接指向Mozilla文档,因为它相当不错,但是它们不是Mozilla特定的功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.