如何在Javascript中将字符串从下划线或虚线格式转换为camelCase或UpperCamelCase?

时间:2015-09-04 07:11:15

标签: javascript string camelcasing

如何使用虚线表示法的字符串:

something-like-this

或下划线表示法:

something_like_this

并将其转换为camelCase:

somethingLikeThis

或UpperCamelCase:

SomethingLikeThis

编辑添加 @torazaburo指责这是Converting any string into camel case的副本但是,这个问题并没有解决这里有问题的输入,虚线或下划线。 @torazaburo,请撤销你的诬告。

3 个答案:

答案 0 :(得分:3)

对于那些不想与当地人混淆的人:

toCamelCase = function(string){
    return string.replace(/[_\-]([^_\-])/g, function($0,$1){return $1.toUpperCase();});
}
toUpperCamelCase = function(string){
    return string.charAt(0).toUpperCase() + toCamelCase(string.substring(1));
}

或者那些舒适的扩展本地人:

if (!String.prototype.toCamelCase)String.prototype.toCamelCase = function(){
    return this.replace(/[_\-]([^_\-])/g, function($0,$1){return $1.toUpperCase();});
};
if (!String.prototype.toUpperCamelCase)String.prototype.toUpperCamelCase = function(){
    return this.charAt(0).toUpperCase() + this.substring(1).toCamelCase();
};

小提琴: http://jsfiddle.net/trex005/akj0hx7b/

答案 1 :(得分:1)

trex005的答案已经足够好了,但是我的输入是用虚线,下划线,camelCase或UpperCamelCase输入的。你还不确定你换了一个字符串了吗?转换它!不用担心!

对于那些不想与当地人混淆的人:

toCamelCase = function(string){
    string = string.replace(/[_\-A-Z]([^_\-A-Z])/g, function($0,$1){return $0.charAt(0).match(/[A-Z]/)?$0:$1.toUpperCase();});
    return string.charAt(0).toLowerCase() + string.substring(1);
}
toUpperCamelCase = function(string){
    string = toCamelCase(string);
    return string.charAt(0).toUpperCase() + string.substring(1);
}

或者那些舒适的扩展本地人

if (!String.prototype.toCamelCase)String.prototype.toCamelCase = function(){
    string = this.replace(/[_\-A-Z]([^_\-A-Z])/g, function($0,$1){return $0.charAt(0).match(/[A-Z]/)?$0:$1.toUpperCase();});
    return string.charAt(0).toLowerCase() + string.substring(1);
}
if (!String.prototype.toUpperCamelCase)String.prototype.toUpperCamelCase = function(){
    string = toCamelCase(this);
    return string.charAt(0).toUpperCase() + string.substring(1);
}

小提琴: http://jsfiddle.net/trex005/akj0hx7b/1/

答案 2 :(得分:1)

trex005's answer很好,trex005's addition to it更方便,但是如果你要去那么远,为什么不一直走,让你来回转换虚线,下划线,camelCase和UpperCamelCase是否需要?

对于那些不想与当地人混淆的人:

formatParts = function(string){
    var parts = [];
    string.replace(/([_\-A-Z]|^[^_\-A-Z])([^_\-A-Z]*)/g,function($0,$1,$2){
        var part = $1.match(/[_\-]/)?$2:$1+$2;
        parts.push(part.toLowerCase());
    });
    return parts;
}
toCamelCase = function(string){
    var output = '';
    formatParts(string).forEach(function(part,index){
        output += index?part.charAt(0).toUpperCase() + part.substring(1):part;
    });
    return output;
}
toUpperCamelCase = function(string){
    var output = '';
    formatParts(string).forEach(function(part,index){
        output += part.charAt(0).toUpperCase() + part.substring(1);
    });
    return output;
}
toDashed = function(string){
    return formatParts(string).join('-');
}
toUnderscored = function(string){
    return formatParts(string).join('_');
}

或者那些舒适的扩展本地人:

if (!String.prototype.formatParts)String.prototype.formatParts = function(){
    var parts = [];
    this.replace(/([_\-A-Z]|^[^_\-A-Z])([^_\-A-Z]*)/g,function($0,$1,$2){
        var part = $1.match(/[_\-]/)?$2:$1+$2;
        parts.push(part.toLowerCase());
    });
    return parts;
}
if (!String.prototype.toCamelCase)String.prototype.toCamelCase = function(){
    var output = '';
    this.formatParts().forEach(function(part,index){
        output += index?part.charAt(0).toUpperCase() + part.substring(1):part;
    });
    return output;
}
if (!String.prototype.toUpperCamelCase)String.prototype.toUpperCamelCase = function(){
    var output = '';
    this.formatParts().forEach(function(part,index){
        output += part.charAt(0).toUpperCase() + part.substring(1);
    });
    return output;
}
if (!String.prototype.toDashed)String.prototype.toDashed = function(){
    return this.formatParts().join('-');
}
if (!String.prototype.toUnderscored)String.prototype.toUnderscored = function(){
    return this.formatParts().join('_');
}

小提琴: http://jsfiddle.net/trex005/akj0hx7b/3/