如何使用javascript正则表达式将字符串转换为驼峰大小写?
设备类名称或 设备类名或设备类名或设备类名
应该全部变成:equipmentClassName。
如何使用javascript正则表达式将字符串转换为驼峰大小写?
设备类名称或 设备类名或设备类名或设备类名
应该全部变成:equipmentClassName。
当前回答
不要使用String.prototype. tocamelcase(),因为String。原型是只读的,大多数js编译器会给你这个警告。
像我一样,那些知道字符串总是只包含一个空格的人可以使用一种更简单的方法:
let name = 'test string';
let pieces = name.split(' ');
pieces = pieces.map((word, index) => word.charAt(0)[index===0 ? 'toLowerCase' :'toUpperCase']() + word.toLowerCase().slice(1));
return pieces.join('');
祝你有愉快的一天。:)
其他回答
此函数通过通过cammelcase等这些测试
Foo酒吧 ——foo bar __FOO_BAR__ - foo123Bar foo_Bar
function toCamelCase(str) { var arr= str.match(/[a-z]+|\d+/gi); return arr.map((m,i)=>{ let low = m.toLowerCase(); if (i!=0){ low = low.split('').map((s,k)=>k==0?s.toUpperCase():s).join`` } return low; }).join``; } console.log(toCamelCase('Foo Bar')); console.log(toCamelCase('--foo-bar--')); console.log(toCamelCase('__FOO_BAR__-')); console.log(toCamelCase('foo123Bar')); console.log(toCamelCase('foo_Bar')); console.log(toCamelCase('EquipmentClass name')); console.log(toCamelCase('Equipment className')); console.log(toCamelCase('equipment class name')); console.log(toCamelCase('Equipment Class Name'));
一个超级简单的方法,使用turboCommons库:
npm install turbocommons-es5
<script src="turbocommons-es5/turbocommons-es5.js"></script>
<script>
var StringUtils = org_turbocommons.StringUtils;
console.log(StringUtils.formatCase('EquipmentClass', StringUtils.FORMAT_LOWER_CAMEL_CASE));
console.log(StringUtils.formatCase('Equipment className', StringUtils.FORMAT_LOWER_CAMEL_CASE));
console.log(StringUtils.formatCase('equipment class name', StringUtils.FORMAT_LOWER_CAMEL_CASE));
console.log(StringUtils.formatCase('Equipment Class Name', StringUtils.FORMAT_LOWER_CAMEL_CASE));
</script>
你也可以使用StringUtils。FORMAT_CAMEL_CASE和StringUtils。FORMAT_UPPER_CAMEL_CASE生成首字母大小写的变化。
更多信息:
将字符串转换为驼峰,UpperCamelCase或lowerCamelCase
从上驼峰格式("TestString")到下驼峰格式("TestString"),而不使用正则表达式(让我们面对现实吧,正则表达式是邪恶的):
'TestString'.split('').reduce((t, v, k) => t + (k === 0 ? v.toLowerCase() : v), '');
编辑:现在工作在IE8没有变化。
编辑:我对camelCase到底是什么持少数意见(主角小写vs大写)。一般来说,社区认为开头小写字母是camel大小写,开头大写字母是pascal大小写。我创建了两个只使用正则表达式模式的函数。所以我们使用统一的词汇我已经改变了我的立场以配合大多数人。
我相信在这两种情况下你只需要一个正则表达式:
var camel = " THIS is camel case "
camel = $.trim(camel)
.replace(/[^A-Za-z]/g,' ') /* clean up non-letter characters */
.replace(/(.)/g, function(a, l) { return l.toLowerCase(); })
.replace(/(\s.)/g, function(a, l) { return l.toUpperCase(); })
.replace(/[^A-Za-z\u00C0-\u00ff]/g,'');
// Returns "thisIsCamelCase"
or
var pascal = " this IS pascal case "
pascal = $.trim(pascal)
.replace(/[^A-Za-z]/g,' ') /* clean up non-letter characters */
.replace(/(.)/g, function(a, l) { return l.toLowerCase(); })
.replace(/(^.|\s.)/g, function(a, l) { return l.toUpperCase(); })
.replace(/[^A-Za-z\u00C0-\u00ff]/g,'');
// Returns "ThisIsPascalCase"
在函数中:你会注意到,在这些函数中,replace是将任何非a-z与空格与空字符串交换。这是为大写创建单词边界。"hello-MY#world" -> "HelloMyWorld"
// remove \u00C0-\u00ff] if you do not want the extended letters like é
function toCamelCase(str) {
var retVal = '';
retVal = $.trim(str)
.replace(/[^A-Za-z]/g, ' ') /* clean up non-letter characters */
.replace(/(.)/g, function (a, l) { return l.toLowerCase(); })
.replace(/(\s.)/g, function (a, l) { return l.toUpperCase(); })
.replace(/[^A-Za-z\u00C0-\u00ff]/g, '');
return retVal
}
function toPascalCase(str) {
var retVal = '';
retVal = $.trim(str)
.replace(/[^A-Za-z]/g, ' ') /* clean up non-letter characters */
.replace(/(.)/g, function (a, l) { return l.toLowerCase(); })
.replace(/(^.|\s.)/g, function (a, l) { return l.toUpperCase(); })
.replace(/[^A-Za-z\u00C0-\u00ff]/g, '');
return retVal
}
注:
为了可读性,我保留了A-Za-z vs为模式(/[^A-Z]/ig)添加大小写不敏感标志(I)。 这可以在IE8中工作(srsly,谁还在使用IE8呢。)使用我在IE11、IE10、IE9、IE8、IE7和IE5中测试过的(F12)开发工具。适用于所有文档模式。 这将正确地对以空格或不以空格开头的字符串的第一个字母进行大小写区分。
享受
您可以使用以下解决方案:
String.prototype.toCamelCase = function(){ return this.replace(/\s(\w)/ig, function(all, letter){return letter. touppercase ();}) .replace (/ (^ \ w)、功能(1美元){返回1.美元tolowercase ()}); }; console.log(“设备名称”.toCamelCase ());