views:

87

answers:

1

I have been using jQuery address plugin and it passes an event.value which might result in /messages/inbox/. I want to be able to turn that into Messages Inbox.

I am not sure which regex to use and how to do this. Currently I have this, but this is just way too messy for me.

var href = event.value != '/' ? event.value : '/wall/';
var title1 = href.replace('/', "");
var title2 = title1.replace('/', " ");
var myTitle = title2.replace('/', "");
$.address.title("My-Site | " + myTitle);
+4  A: 

This is a little tidier; lop off the start and end characters, then replace the middle, then run a regex replace to swap the characters for uppercase versions:

var href = event.value != '/' ? event.value : '/wall/';
    title = href.slice(1,-1).replace("/", " "),
    myTitle = title.replace(/\b[a-z]/g,function($0){ return $0.toUpperCase(); });

$.address.title("My-Site | " + myTitle);

Methods used:

Andy E
Nice. Beware of the limitations of JavaScript regular expressions: only A-Z, a-z, 0-9, and _ are considered word characters, so words starting in other characters such as accented letters will not be capitalized correctly.
Tim Down
@Tim: a very good point, thank you :-)
Andy E