js实现a_b变成A B的两种方法

时间:2021-01-29 02:41:57

1.var key = 'a_b';

var a = key.replace(/\b.|_./g, function (i) {
if (i.length === 2) {
i = ' ' + i[1];
}
return i.toUpperCase();
});

2.先去除下划线,再将首字母大写,再用substring方法,最后用join,like this:

var array = key.split('_');

for (var i = 0; i < array.length; i++) {
array[i] = array[i][0].toUpperCase() + array[i].substring(1, array[i].length);
}
var servertype = array.join(' ');