jQuery: get the numeric part of an ID

by gabrieleromanato

HTML

<p id="test1">Test</p>
<div id="foo-2">Test</div>

JavaScript

var getNumericPart = function(id) {
    var $num = id.replace(/[^\d]+/, '');
    return $num;
}
    
    
var test1 = $('#test1').attr('id');
alert(getNumericPart(test1));

var foo2 = $('#foo-2').attr('id');
alert(getNumericPart(foo2));