如何使用javascript正则表达式将字符串转换为驼峰大小写?
设备类名称或 设备类名或设备类名或设备类名
应该全部变成:equipmentClassName。
如何使用javascript正则表达式将字符串转换为驼峰大小写?
设备类名称或 设备类名或设备类名或设备类名
应该全部变成:equipmentClassName。
当前回答
可靠的高性能示例:
function camelize(text) {
const a = text.toLowerCase()
.replace(/[-_\s.]+(.)?/g, (_, c) => c ? c.toUpperCase() : '');
return a.substring(0, 1).toLowerCase() + a.substring(1);
}
情况下,修改字符:
连字符- 下划线_ 时期。 空间
其他回答
return "hello world".toLowerCase().replace(/(?:(^.)|(\s+.))/g, function(match) {
return match.charAt(match.length-1).toUpperCase();
}); // HelloWorld
以下是我的建议:
function toCamelCase(string) {
return `${string}`
.replace(new RegExp(/[-_]+/, 'g'), ' ')
.replace(new RegExp(/[^\w\s]/, 'g'), '')
.replace(
new RegExp(/\s+(.)(\w+)/, 'g'),
($1, $2, $3) => `${$2.toUpperCase() + $3.toLowerCase()}`
)
.replace(new RegExp(/\s/, 'g'), '')
.replace(new RegExp(/\w/), s => s.toLowerCase());
}
or
String.prototype.toCamelCase = function() {
return this
.replace(new RegExp(/[-_]+/, 'g'), ' ')
.replace(new RegExp(/[^\w\s]/, 'g'), '')
.replace(
new RegExp(/\s+(.)(\w+)/, 'g'),
($1, $2, $3) => `${$2.toUpperCase() + $3.toLowerCase()}`
)
.replace(new RegExp(/\s/, 'g'), '')
.replace(new RegExp(/\w/), s => s.toLowerCase());
};
测试用例:
describe('String to camel case', function() {
it('should return a camel cased string', function() {
chai.assert.equal(toCamelCase('foo bar'), 'fooBar');
chai.assert.equal(toCamelCase('Foo Bar'), 'fooBar');
chai.assert.equal(toCamelCase('fooBar'), 'fooBar');
chai.assert.equal(toCamelCase('FooBar'), 'fooBar');
chai.assert.equal(toCamelCase('--foo-bar--'), 'fooBar');
chai.assert.equal(toCamelCase('__FOO_BAR__'), 'fooBar');
chai.assert.equal(toCamelCase('!--foo-¿?-bar--121-**%'), 'fooBar121');
});
});
简单容易理解这段代码,希望这对你有帮助,我用下面的逻辑解决了我的问题
// 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"));
我希望上面的例子能解决你的问题。
快速的方法
function toCamelCase(str) {
return str[0].toUpperCase() + str.substr(1).toLowerCase();
}
此函数通过通过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'));