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')
asked Sep 24, 2011 at 21:21
4
  • 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?) Commented Sep 24, 2011 at 21:24
  • 1
    Why are you doing this parseInt("1ドル") ? That will give you NaN Commented Sep 24, 2011 at 21:25
  • @Brad, title - is the title of the page, that selector should work. Commented Sep 24, 2011 at 21:26
  • @Interstellar_Coder: Touche, I had tunnel-vision and was thinking of only the body's contents. Good call. Commented Sep 24, 2011 at 21:30

3 Answers 3

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.

answered Sep 24, 2011 at 21:40

3 Comments

+1 I was not aware of the function as a 2nd argument. This is a neat solution.
Thanks, this worked! Also thanks for mentioning that I shouldn't assume that parseInt will assume base 10 by default in all browsers.
@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 :)
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/

answered Sep 24, 2011 at 21:27

Comments

0

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

answered Sep 24, 2011 at 21:24

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.