jQuery将每个单词的首字母大写

jQuery将每个单词的首字母大写

本文介绍了jQuery将每个单词的首字母大写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用jQuery地址插件,它传递了一个event.value,可能会导致/messages/inbox/.我希望能够将其转换为Messages Inbox.

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

使用的方法:

这篇关于jQuery将每个单词的首字母大写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 23:39