wrap first n words with span

wrap first n words with span

Hey,

I want to wrap a certain amount of words in every h1 with a span.
The amount of words that I want to wrap are noted in the class of the headings (.hh- n ).

So my html looks like:

  1. <h1 class="hh-1">wrap the first word</h1>
  2. <h1 class="hh-2">wrap the first two words</h1>
  3. <h1 class="hh-3">wrap the first three words</h1>
  4. <h1 class="hh-4">wrap the first four words</h1>
  1. $('h1').each(function() {
  2.       hhNumber = $(this).attr('class').substring(4, 3);
  3.       // ??
  4. });

please help!

best regards.