1

Never seem to master those regex's...

I need to update the total with newVal

<span class="myClass">some text Total (12)</span>

into

<span class="myClass">some text Total (13)</span>

and also without a current value

<span class="myClass">some text Total</span>

into

<span class="myClass">some text Total (13)</span>

oh, and some text can be anything

my code

    newVal = 13;
    $('.myClass').text( $('.myClass').text().replace(???, ???) );
1
  • Which regexes have you tried? Commented Jul 13, 2012 at 11:14

2 Answers 2

1

You could wrap counters with span:

<span class="myClass">some text Total (<span>12</span>)</span>

and do like so:

newVal = 13;
$('.myClass span').text(newVal);
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry, need to do it without wrapping the counter.
0

Wrong approach, your code would only return the new string. To set it, use

$('.myClass').text(function(i, old){ 
   return old.replace(/(\s+\(\d+\))?$/, " ("+newVal+")");
})

The regex matches the string end, and (optionally) whitespaces + bracketed number before.

1 Comment

It does only delete the " (12)" in the end, which you want to have replaced, doesn't it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.