Untested, but you may need to use the non-greedy form of .* to make this work:
/.*?(\d+).*/ JK -----Original Message----- From: jquery-en@googlegroups.com [mailto:jquery...@googlegroups.com] On Behalf Of Ricardo Sent: Monday, May 11, 2009 1:09 PM To: jQuery (English) Subject: [jQuery] Re: regex backreference with text()? On May 11, 2:23 pm, waseem sabjee <waseemsab...@gmail.com> wrote: > var t = $("#myid").text(); > $ ("#myid").text(t.replace(' left in stock',''); > t = $("#myid").text(); > if(t == "1") { > $ ("#myid").text(t.replace(t,'only '+t+' item remain');} else { > > $ ("#myid").text(t.replace(t,t+' items remaining'); > > } There are simpler and more robust ways to do that than replacing the exact phrase: Doing a regex replace using a back-reference: var $stock = $('#myel'); $stock.text( $stock.text().replace(/.*(\d+).*/, 'Only $1 remaining') ); // $1 is a back reference to the (last) digit matched in the string, in between parenthesis Or simply grabbing the number: var $stock = $('#myel'); var num = /\d+/.exec( $stock.text() )[0]; $stock.text('Only '+num+' remaining'); cheers -- ricardo