简体   繁体   中英

jQuery capitilize first letter each word

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);

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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM