1

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

Rajaprabhu Aravindasamy
  • 66,513
  • 17
  • 101
  • 130
user2679140
  • 153
  • 2
  • 14
  • Your question and title conflict. Do you want to remove all letters (`a-z` and `A-Z`), or all non-numeric (i.e. digit) characters (anything that isn't `0-9`)? You example removes a space character, which indicates the latter, which agrees with the title but conflicts with the question's content. – ajp15243 Feb 02 '14 at 07:59
  • possible duplicate of [jQuery remove all characters but numbers and decimals](http://stackoverflow.com/questions/15464306/jquery-remove-all-characters-but-numbers-and-decimals) – sdespont Feb 02 '14 at 08:09

3 Answers3

4

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, ''); 
})

DEMO

Rajaprabhu Aravindasamy
  • 66,513
  • 17
  • 101
  • 130
  • @DavidThomas Sorry, I am a non-native English speaker, I cant get what did you mean.? :) – Rajaprabhu Aravindasamy Feb 02 '14 at 08:04
  • 1
    Using all-capitals is usually interpreted as shouting when in an online setting; I was wondering *why* you you were shouting 'DEMO' instead of simply using normal writing conventions, 'Demo,' for example. – David Thomas Feb 02 '14 at 08:52
0

You could also strip all the non-digit characters (\D or [^0-9]):

'abc123cdef4567hij89'.replace(/\D/g, ''); // returns '123456789'

Avi L
  • 1,558
  • 2
  • 15
  • 33
0

Here is an example

$('h1').each(function(){
    $(this).html($(this).html().replace(/[^0-9\.]/g, ''));
});

DEMO

sdespont
  • 13,915
  • 9
  • 56
  • 97