如何使用javascript正则表达式将字符串转换为驼峰大小写?
设备类名称或 设备类名或设备类名或设备类名
应该全部变成:equipmentClassName。
如何使用javascript正则表达式将字符串转换为驼峰大小写?
设备类名称或 设备类名或设备类名或设备类名
应该全部变成:equipmentClassName。
当前回答
此函数通过通过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'));
其他回答
可靠的高性能示例:
function camelize(text) {
const a = text.toLowerCase()
.replace(/[-_\s.]+(.)?/g, (_, c) => c ? c.toUpperCase() : '');
return a.substring(0, 1).toLowerCase() + a.substring(1);
}
情况下,修改字符:
连字符- 下划线_ 时期。 空间
我认为这应该可行。
function cammelCase(str){
let arr = str.split(' ');
let words = arr.filter(v=>v!='');
words.forEach((w, i)=>{
words[i] = w.replace(/\w\S*/g, function(txt){
return txt.charAt(0).toUpperCase() + txt.substr(1);
});
});
return words.join('');
}
以下所有14个排列产生相同的“equipmentClassName”结果。
String.prototype.toCamelCase = function() { return this.replace(/[^a-z ]/ig, '') // Replace everything but letters and spaces. .replace(/(?:^\w|[A-Z]|\b\w|\s+)/g, // Find non-words, uppercase letters, leading-word letters, and multiple spaces. function(match, index) { return +match === 0 ? "" : match[index === 0 ? 'toLowerCase' : 'toUpperCase'](); }); } String.toCamelCase = function(str) { return str.toCamelCase(); } var testCases = [ "equipment class name", "equipment class Name", "equipment Class name", "equipment Class Name", "Equipment class name", "Equipment class Name", "Equipment Class name", "Equipment Class Name", "equipment className", "equipment ClassName", "Equipment ClassName", "equipmentClass name", "equipmentClass Name", "EquipmentClass Name" ]; for (var i = 0; i < testCases.length; i++) { console.log(testCases[i].toCamelCase()); };
看看你的代码,你可以实现它只有两个替换调用:
function camelize(str) {
return str.replace(/(?:^\w|[A-Z]|\b\w)/g, function(word, index) {
return index === 0 ? word.toLowerCase() : word.toUpperCase();
}).replace(/\s+/g, '');
}
camelize("EquipmentClass name");
camelize("Equipment className");
camelize("equipment class name");
camelize("Equipment Class Name");
// all output "equipmentClassName"
编辑:或者使用一个替换调用,捕获RegExp中的空白。
function camelize(str) {
return str.replace(/(?:^\w|[A-Z]|\b\w|\s+)/g, function(match, index) {
if (+match === 0) return ""; // or if (/\s+/.test(match)) for white spaces
return index === 0 ? match.toLowerCase() : match.toUpperCase();
});
}
您可以使用以下解决方案:
String.prototype.toCamelCase = function(){ return this.replace(/\s(\w)/ig, function(all, letter){return letter. touppercase ();}) .replace (/ (^ \ w)、功能(1美元){返回1.美元tolowercase ()}); }; console.log(“设备名称”.toCamelCase ());