如何在Javascript中将字符串从下划线或虚线格式转换为驼峰大小写或上驼峰大小写?
How can I convert a string from underscore or dashed format to camelCase or UpperCamelCase in Javascript?
如何使用虚线表示的字符串:
something-like-this
或下划线符号:
something_like_this
somethingLikeThis
或UpperCamelCase:
SomethingLikeThis
?
编辑添加 @torazaburo指责这是将任何字符串转换为驼色大小写的副本,然而,该问题并没有解决输入中的任何一个,虚线或下划线,在这里有问题。@torazaburo,请撤销你的诬告。
对于那些不想与本地用户打交道的人:
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/
trex005的答案足够好,但我的输入是虚线、下划线、驼线大小写或驼线大小写。不确定是否转换了字符串?再转换一次!没烦恼!
对于那些不想和当地人打交道的人:
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);
}
或者对于那些舒适的扩展native
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/
trex005的答案很好,并且trex005的附加功能更加方便,但是如果您要走那么远,为什么不允许您根据需要在虚线,下划线,驼线大小写和驼线大小写之间来回转换呢?
对于那些不想和当地人打交道的人:
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/
相关文章:
- 如何在JavaScript中将字符串转换为函数引用
- 如何在Javascript中将JSon对象转换为数组
- 使用JS将数组转换为json对象
- 如何使用json将对象列表从java转换为javascript
- 偶尔结结巴巴地说“;堆叠的”;translate()上的转换(v4.0.0-alpha40)
- 为什么在单独的函数中应用时转换会闪烁/断断续续(D3)
- 在Javascript中转换对象数组
- 将数字转换为一定数量的硬币
- Javascript 将 unicode 字符串转换为“标题大小写”
- 字符串大小写转换 javascript
- 将驼峰大小写字符串转换为连字符的角度服务
- 将句子转换为 InitCap / 骆驼大小写 / 正确大小写
- 将句子或驼峰大小写单词转换为棘壳
- 应用不区分大小写的正则表达式后,将大写字符转换回大写
- 在javascript中将字符串转换为句子大小写
- 如果字符串具有不同的大小写,如何将字符串中每个单词的首字母转换为大写
- 如何使用Javascript中的map函数将混合大小写数组转换为小写数组?
- 如何在Javascript中将字符串从下划线或虚线格式转换为驼峰大小写或上驼峰大小写?
- 将驼峰大小写文本转换为标题大小写文本
- 在Google Apps脚本中转换字符串到正确的/标题大小写