将camelCaseText转换为句子案例文本 [英] Convert camelCaseText to Sentence Case Text

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

问题描述

如何在JavaScript中将'helloThere'或'HelloThere'之类的字符串转换为'Hello There'?

How can I convert a string either like 'helloThere' or 'HelloThere' to 'Hello There' in JavaScript?

推荐答案

var text = 'helloThereMister';
var result = text.replace( /([A-Z])/g, " $1" );
var finalResult = result.charAt(0).toUpperCase() + result.slice(1); // capitalize the first letter - as an example.

请注意$ 1中的空格。

编辑:添加了第一个字母大写的示例。当然,如果第一个字母已经是大写字母 - 您将有一个空余要删除。

added an example of capitalization of the first letter. Of course, in case the first letter is already capital - you would have a spare space to remove.

这篇关于将camelCaseText转换为句子案例文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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