jQuery标题案例 [英] jQuery Title Case

查看:132
本文介绍了jQuery标题案例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个内置的方式与jQuery标题案例一个字符串?所以考虑到像bob smith这样的东西,它变成了Bob Smith?

Is there a built in way with jQuery to "title case" a string? So given something like bob smith, it turns into "Bob Smith"?

推荐答案

你不需要jQuery这个;它可以使用原生 .replace()来完成 方法:

You don't need jQuery for this; it can be accomplished using the native .replace() method:

function toTitleCase(str) {
    return str.replace(/(?:^|\s)\w/g, function(match) {
        return match.toUpperCase();
    });
}

alert(toTitleCase("foo bar baz")); // alerts "Foo Bar Baz"

这篇关于jQuery标题案例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆