Pregunta

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.

¿Fue útil?

Solución

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); 

Otros consejos

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

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top