如何使用javascript正则表达式将字符串转换为驼峰大小写?
设备类名称或 设备类名或设备类名或设备类名
应该全部变成:equipmentClassName。
如何使用javascript正则表达式将字符串转换为驼峰大小写?
设备类名称或 设备类名或设备类名或设备类名
应该全部变成:equipmentClassName。
当前回答
这就是我的解决方案:
const toCamelWord = (word, idx) => Idx === 0 ? word.toLowerCase (): word.charAt(0).toUpperCase() + word.slice(1).toLowerCase(); const toCamelCase = text => 文本 .split (/ [_ - \] + /) . map (toCamelWord) . join (" "); console.log (toCamelCase(用户ID))
其他回答
如果有人正在使用lodash,则有_.camelCase()函数。
_.camelCase('Foo Bar');
// → 'fooBar'
_.camelCase('--foo-bar--');
// → 'fooBar'
_.camelCase('__FOO_BAR__');
// → 'fooBar'
这建立在CMS的答案上,通过删除包括下划线在内的任何非字母字符,\w不会删除这些字符。
function toLowerCamelCase(str) {
return str.replace(/[^A-Za-z0-9]/g, ' ').replace(/^\w|[A-Z]|\b\w|\s+/g, function (match, index) {
if (+match === 0 || match === '-' || match === '.' ) {
return ""; // or if (/\s+/.test(match)) for white spaces
}
return index === 0 ? match.toLowerCase() : match.toUpperCase();
});
}
toLowerCamelCase("EquipmentClass name");
toLowerCamelCase("Equipment className");
toLowerCamelCase("equipment class name");
toLowerCamelCase("Equipment Class Name");
toLowerCamelCase("Equipment-Class-Name");
toLowerCamelCase("Equipment_Class_Name");
toLowerCamelCase("Equipment.Class.Name");
toLowerCamelCase("Equipment/Class/Name");
// All output e
我知道这是一个老答案,但这处理空格和_ (lodash)
function toCamelCase(s){
return s
.replace(/_/g, " ")
.replace(/\s(.)/g, function($1) { return $1.toUpperCase(); })
.replace(/\s/g, '')
.replace(/^(.)/, function($1) { return $1.toLowerCase(); });
}
console.log(toCamelCase("Hello world");
console.log(toCamelCase("Hello_world");
// Both print "helloWorld"
简单容易理解这段代码,希望这对你有帮助,我用下面的逻辑解决了我的问题
// This example is for React Js User
const ConverToCamelCaseString = (StringValues)=>
{
let WordsArray = StringValues.split(" ");
let CamelCaseValue = '';
for (let index = 0; index < WordsArray.length; index++)
{
let singleWord = WordsArray[index];
singleWord.charAt(0).toUpperCase();
singleWord =singleWord.charAt(0).toUpperCase() + singleWord.slice(1);
CamelCaseValue +=" "+singleWord;
}
CamelCaseValue = CamelCaseValue.trim();
return CamelCaseValue;
}
下面的例子是针对核心javaScript用户的
function ConverToCamelCaseString (StringValues)
{
let WordsArray = StringValues.split(" ");
let CamelCaseValue = '';
for (let index = 0; index < WordsArray.length; index++)
{
let singleWord = WordsArray[index];
singleWord.charAt(0).toUpperCase();
singleWord =singleWord.charAt(0).toUpperCase() + singleWord.slice(1);
CamelCaseValue +=" "+singleWord;
}
CamelCaseValue = CamelCaseValue.trim();
return CamelCaseValue;
}
console.log(ConverToCamelCaseString("this is my lower case string"));
我希望上面的例子能解决你的问题。
以下所有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()); };