如何使用javascript正则表达式将字符串转换为驼峰大小写?
设备类名称或 设备类名或设备类名或设备类名
应该全部变成:equipmentClassName。
如何使用javascript正则表达式将字符串转换为驼峰大小写?
设备类名称或 设备类名或设备类名或设备类名
应该全部变成:equipmentClassName。
当前回答
这将把任何空格大小写字符串转换为thisWordsInCamelCase
function toCamelCase(str) {
return str.toString() && str.split(' ').map((word, index) => {
return (index === 0 ? word[0].toLowerCase() : word[0].toUpperCase()) + word.slice(1).toLowerCase()
}).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(text) {
const a = text.toLowerCase()
.replace(/[-_\s.]+(.)?/g, (_, c) => c ? c.toUpperCase() : '');
return a.substring(0, 1).toLowerCase() + a.substring(1);
}
情况下,修改字符:
连字符- 下划线_ 时期。 空间
斯科特的回答稍作修改:
toCamelCase = (string) ->
string
.replace /[\s|_|-](.)/g, ($1) -> $1.toUpperCase()
.replace /[\s|_|-]/g, ''
.replace /^(.)/, ($1) -> $1.toLowerCase()
现在它也替换了'-'和'_'。
为了有效地创建一个将字符串的大小写转换为驼峰式大小写的函数,该函数还需要首先将每个字符串转换为小写,然后再将非第一个字符串的第一个字符转换为大写字母。
我的示例字符串是:
"text That I WaNt to make cAMEL case"
对于这个问题提供的许多其他解决方案返回这个:
"textThatIWaNtToMakeCAMELCase"
我认为应该是预期的,期望的输出将是这样的,其中所有的中间字符串大写字符首先转换为小写:
"textThatIWanrtToMakeCamelCase"
这可以在不使用任何replace()方法调用的情况下完成,通过使用String.prototype.split(), Array.prototype.map()和Array.prototype.join()方法:
≤ES5版本
function makeCamelCase(str) {
return str
.split(' ')
.map((e,i) => i
? e.charAt(0).toUpperCase() + e.slice(1).toLowerCase()
: e.toLowerCase()
)
.join('')
}
makeCamelCase("text That I WaNt to make cAMEL case")
// -> "textThatIWanrtToMakeCamelCase" ✅
我将分解每一行的功能,然后以其他两种格式提供相同的解决方案——ES6格式和字符串格式。prototype方法,不过我建议不要像这样直接扩展内置的JavaScript原型。
讲解员
function makeCamelCase(str) {
return str
// split string into array of different words by splitting at spaces
.split(' ')
// map array of words into two different cases, one for the first word (`i == false`) and one for all other words in the array (where `i == true`). `i` is a parameter that denotes the current index of the array item being evaluated. Because indexes start at `0` and `0` is a "falsy" value, we can use the false/else case of this ternary expression to match the first string where `i === 0`.
.map((e,i) => i
// for all non-first words, use a capitalized form of the first character + the lowercase version of the rest of the word (excluding the first character using the slice() method)
? e.charAt(0).toUpperCase() + e.slice(1).toLowerCase()
// for the first word, we convert the entire word to lowercase
: e.toLowerCase()
)
// finally, we join the different strings back together into a single string without spaces, our camel-cased string
.join('')
}
makeCamelCase("text That I WaNt to make cAMEL case")
// -> "textThatIWanrtToMakeCamelCase" ✅
压缩ES6+(一行程序)版本
const makeCamelCase = str => str.split(' ').map((e,i) => i ? e.charAt(0).toUpperCase() + e.slice(1).toLowerCase() : e.toLowerCase()).join('')
makeCamelCase("text That I WaNt to make cAMEL case")
// -> "textThatIWanrtToMakeCamelCase" ✅
字符串。原型方法版本
String.prototype.toCamelCase = function() {
return this
.split(' ')
.map((e,i) => i
? e.charAt(0).toUpperCase() + e.slice(1).toLowerCase()
: e.toLowerCase()
)
.join('')
}
"text That I WaNt to make cAMEL case".toCamelCase()
// -> "textThatIWanrtToMakeCamelCase" ✅
大多数答案不处理unicode字符,例如重音字符。
如果你想处理unicode和重音,在任何现代浏览器中都可以使用以下方法:
camelCase = s => s
.replace( /(?<!\p{L})\p{L}|\s+/gu,
m => +m === 0 ? "" : m.toUpperCase() )
.replace( /^./,
m => m?.toLowerCase() );
以下是一些解释:
因为问题要求第一个字符是小写的,所以必须调用第二个replace。 第一个replace调用标识任何跟在任何非字母后面的unicode字母(相当于\b\w,但适用于非ASCII字母)。为此,u标志(unicode)是必要的。
注意,这将保持大写字母不变。如果您的输入文本包含首字母缩略词,这很有用。
e.g.
console.log(camelCase("Shakespeare in FR is être ou ne pas être");
// => 'ShakespeareInFRIsÊtreOuNePasÊtre'
如果您想要纯驼峰大小写,其中首字母缩写变成小写,您可以先将输入文本小写。