下面的代码是我的实现,不知道有没有更好的实现方案。
javascript
String.prototype.firstUpperCase = function(){ return this.replace(/\b(\w)(\w*)/g, function($0, $1, $2) { return $1.toUpperCase() + $2.toLowerCase(); }); }
求接龙。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
CSS 完全搞定啊 text-transform: capitalize;
这么简单的功能 还用js 就太浪费累
其实有很多方法。
最常见的,最基础的:
1.先split()成数组。
2.将数组中的元素全部小写化。(toLowerCase())
3.使用replace(),toUpperCase()方法将每一项首字母大写化,之后join()就可以了。
但是这样确实很麻烦。
正在学习ES6中,ES6里面解决这个方案这样写就可以了。
是不是觉得很简洁?
不改变原字符串。(暂时想不到改变原字符串的方式,因为我记得字符串是只读的)
楼主如果用正则表达式的话,下面的表达应该更简单
why not use CSS? text-transform: capitalize;can solve this.
you can also use this function:
but if only want to change the initials of the whole strings,try this: