Reputation: 77
I need to extract number from string like
<p id="example">etc etc(5)</p>
<p id="example2">etc etc(-5)</p>
I'm using this code
alert($("#example").text().match(/\((\d+)\)/)[1]);
It's work fine if the number is positive but in the case of a negative number get error
Uncaught TypeError: Cannot read property '1' of null
please help me about that thank's
Upvotes: 1
Views: 6912
Reputation: 34021
Try this instead:
alert($("#example").text().match(/\((-?\d+)\)/)[1]);
That will capture negative numbers as well.
Upvotes: 1