문제

I have this list item tag:

<li id="CurrentPage">page 1 of 24</li>

I am trying to get all of the numbers from it in JavaScript and so far I have tried these:

alert($("#CurrentPage").text().match(/[0-9]/));   (gave me 1)
alert($("#CurrentPage").text().match(/\d+/));     (gave me 1)
alert($("#CurrentPage").text().match(/[0-9]+$/)); (gave me 24)

I really thought the second attempt would get me all of them, please could someone explain please.

도움이 되었습니까?

해결책

This should work with global flag (g):

'page 1 of 24'.match(/\d+/g); // 1, 24

So in your case use:

 var numbers = $("#CurrentPage").text().match(/\d+/g); 

다른 팁

Try

alert($("#CurrentPage").text().match(/\d+/ig))
alert($("#CurrentPage").text().match(/[0-9]+/g));

or better say

alert($("#CurrentPage").text().match(/\d+/g));

g is a global modifier

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top