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);
سؤال
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