-1

If a div has a class of 'motto' and within that div it says, "Celebrating 24 years in the business!" I'd like a script that finds any number mentioned in that div (because it could change next year to 25 for example, and so forth, hence I'm not wanting to get the number specifically), I'd like it to add a span with a class to it. Possible?

The reason is that it pulls this text from the WordPress 'description' in settings, so I can't directly put the number in a span. Thanks!

  • 1
    Can you add a span around the number you need to target? or do you not have control over the content of the div? – Huangism Oct 05 '12 at 19:44
  • Right, I can't add a span because it's pulled form the WordPress 'description' in settings. – Toasterdroid Oct 05 '12 at 19:45
  • well I am sure you can exract the number with regex but I am terrible at regex. Or if the text is the same words, you can exact it using substrings as well. Perhaps tis can help you http://stackoverflow.com/questions/3955345/javascript-jquery-get-number-from-string – Huangism Oct 05 '12 at 19:50

1 Answers1

1

You can do it messily with some string manipulation:

var motto = $('.motto'),
    num = motto.text().match(/\d+/), // regex matching digits
    text = motto.text().replace(num, '<span class="num">'+num+'</span>');

motto.html(text);

demo: http://jsbin.com/uxuhuv/1/edit

tuff
  • 4,895
  • 6
  • 26
  • 43