开发者

line-separated to ucwords in js

开发者 https://www.devze.com 2023-03-15 19:15 出处:网络
Does anybody knows how to transform name1-name2-name开发者_运维百科3 to name1Name2Name3 in javascript () ?? (looking for elegant solution)

Does anybody knows how to transform name1-name2-name开发者_运维百科3 to name1Name2Name3 in javascript () ?? (looking for elegant solution)

Thanks


Ok. changed my answer as I now understand what you asked for. If jQuery is an option (just for the map functiuonality oyu can do:

var string2 = $.map(
        string1.split('-'),
        function(str) {
            return str.charAt(0).toUpperCase() + str.slice(1);
        })
    .join('');

You can see a working fiddle here: http://jsfiddle.net/EU6Aa/1/

If jQuery is not an option you can find a simple implementation of array map here


Here's a regex solution, in case you (or anyone else) would prefer that:

var str = 'name1-name2-name3';
var str2 = str.replace(/\-(.)/g, function (m, l) {
    return l.toUpperCase(); 
});
0

精彩评论

暂无评论...
验证码 换一张
取 消