1

I want to replace my title from its default to "*number* new messages | default"

Here is the code I have, it changes from default to 1 new messages fine, but it never goes above 1.

title = $('title').text();
regexp = /^(\d+)/

if (title.match(regexp))
  $('title').text().replace(regexp, (parseInt("$1")+1).toString())
else
  $('title').text('1 New Messages | Default')
LanguagesNamedAfterCofee
  • 5,782
  • 7
  • 45
  • 72
  • Just curious, Why are you using `match` over `test` in this implementation? Also, can you provide an example (or examples) of what `$('title').text()` may have? (Finally, are you sure it's not `#title` or `.title` as the selector?) – Brad Christie Sep 24 '11 at 21:24
  • 1
    Why are you doing this `parseInt("$1")` ? That will give you `NaN` – aziz punjani Sep 24 '11 at 21:25
  • @Brad, title - is the title of the page, that selector should work. – aziz punjani Sep 24 '11 at 21:26
  • @Interstellar_Coder: Touche, I had tunnel-vision and was thinking of only the body's contents. Good call. – Brad Christie Sep 24 '11 at 21:30

3 Answers3

3

You should be using a function as the second argument to replace, you also need to set the new value:

var title = $('title').text();
$('title').text(title.replace(regexp, function(m) { return parseInt(m, 10) + 1) }));

And, as usual, don't forget the radix argument when you call parseInt or you will be unpleasantly surprised sooner or later.

mu is too short
  • 426,620
  • 70
  • 833
  • 800
  • +1 I was not aware of the function as a 2nd argument. This is a neat solution. – Gazler Sep 24 '11 at 21:45
  • Thanks, this worked! Also thanks for mentioning that I shouldn't assume that parseInt will assume base 10 by default in all browsers. – LanguagesNamedAfterCofee Sep 24 '11 at 22:11
  • @LanguagesNamedAfterCofee: I tend to think that leaving the second argument to `parseInt` optional is a bug in the JavaScript spec. You almost never want `parseInt` to guess the radix so parseInt's interface is (IMHO) broken by design. Can't change it now though :) – mu is too short Sep 24 '11 at 22:39
2

I came across this recently and the issue is to do with calling a function within the second argument of the replace function. The $1 is only valid if called directly in a string literal, not as a string argument to a function.

Another issue is that $('title').text().replace(regexp, (parseInt("$1")+1).toString()) will return a string. You need to pass the value you want to assign the text to as a function argument of the text() function like you have done in your else block.

Try this:

title = $('title').text();
regexp = /^(\d+)/

if (numToReplace = title.match(regexp))
  $(title).text(title.replace(regexp, (parseInt(numToReplace[1])+1).toString()))
else
  $('title').text('1 New Messages | Default')

And a JSFiddle: http://jsfiddle.net/KFW4G/

Gazler
  • 83,029
  • 18
  • 279
  • 245
0

replace returns a new string, so you have to use text again to set the text to the result.

Neil
  • 54,642
  • 8
  • 60
  • 72