开发者

Find the number in line. Javascript

How do I find the number in line number may not be开发者_JS百科 in the beginning. For example: "d: \ \ 1.jpg"

Thank you.


You use a Regular Expression with the RegExp object:

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

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


You can use a 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).

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜