0

Possible Duplicate:
Use RegExp to match a parenthetical number then increment it

I know how to implement the solution already but with several ines of code; I'm wondering if there is a short and clever way of accessing the substring of a string that is an integer and incrementing it, where the substring isn't enclosed in any markup tags?

/*Let's say the string is*/ "<span class="ideacount">[5 ideas]</span>"

and I want to increment it to say "[6 ideas]".

Community
  • 1
  • 1
Rob F
  • 529
  • 6
  • 17

1 Answers1

0

Something like this?

var spanContent = $('#ideaCount').text();    
var i = parseInt(spanContent.replace(/[^0-9\.]+/g, ''));
i+=1;
$('#ideaCount').text( spanContent.replace(/[0-9\.]+/g, i+1) );

Just add some error checking.

Vlad
  • 2,475
  • 21
  • 32
  • Should have really thought a bit before posting as I came up with a single-line solution (minus variable declaration): $(this).html(" [" + ((v = $(this).text().match(/\d/) || 0),++v) +" idea" + (v>1?"s":"") + "]"); Unless anyone can make it even leaner? – Rob F Aug 02 '12 at 00:43