I need to strip all letters from a h1 with a certain class so that
<h1 class="hole">1st Hole</h1>
Becomes
<h1 class="hole">1</h1>
And this would replicate on each page, 2nd Hole, 3rd Hole etc
I need to strip all letters from a h1 with a certain class so that
<h1 class="hole">1st Hole</h1>
Becomes
<h1 class="hole">1</h1>
And this would replicate on each page, 2nd Hole, 3rd Hole etc
You can use the receiver function of .text() to accomplish your job efficiently. And additionally ^\d
matches the non-numeric, so we are just replacing the matched non-numeric with ''
Try,
$('h1.hole').text(function(_,xText){
return xText.replace(/[^\d]/g, '');
})
You could also strip all the non-digit characters (\D or [^0-9]):
'abc123cdef4567hij89'.replace(/\D/g, ''); // returns '123456789'