如何使用javascript正则表达式将字符串转换为驼峰大小写?

设备类名称或 设备类名或设备类名或设备类名

应该全部变成:equipmentClassName。


当前回答

简单容易理解这段代码,希望这对你有帮助,我用下面的逻辑解决了我的问题

// 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")); 

我希望上面的例子能解决你的问题。

其他回答

简单容易理解这段代码,希望这对你有帮助,我用下面的逻辑解决了我的问题

// 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 camelCase(str) {
    return (str.slice(0, 1).toLowerCase() + str.slice(1))
      .replace(/([-_ ]){1,}/g, ' ')
      .split(/[-_ ]/)
      .reduce((cur, acc) => {
        return cur + acc[0].toUpperCase() + acc.substring(1);
      });
}

function sepCase(str, sep = '-') {
    return str
      .replace(/[A-Z]/g, (letter, index) => {
        const lcLet = letter.toLowerCase();
        return index ? sep + lcLet : lcLet;
      })
      .replace(/([-_ ]){1,}/g, sep)
}

// All will return 'fooBarBaz'
console.log(camelCase('foo_bar_baz'))
console.log(camelCase('foo-bar-baz'))
console.log(camelCase('foo_bar--baz'))
console.log(camelCase('FooBar  Baz'))
console.log(camelCase('FooBarBaz'))
console.log(camelCase('fooBarBaz'))

// All will return 'foo-bar-baz'
console.log(sepCase('fooBarBaz'));
console.log(sepCase('FooBarBaz'));
console.log(sepCase('foo-bar-baz'));
console.log(sepCase('foo_bar_baz'));
console.log(sepCase('foo___ bar -baz'));
console.log(sepCase('foo-bar-baz'));

// All will return 'foo__bar__baz'
console.log(sepCase('fooBarBaz', '__'));
console.log(sepCase('foo-bar-baz', '__'));

演示在这里:https://codesandbox.io/embed/admiring-field-dnm4r?fontsize=14&hidenavigation=1&theme=dark

斯科特的回答稍作修改:

toCamelCase = (string) ->
  string
    .replace /[\s|_|-](.)/g, ($1) -> $1.toUpperCase()
    .replace /[\s|_|-]/g, ''
    .replace /^(.)/, ($1) -> $1.toLowerCase()

现在它也替换了'-'和'_'。

如果有人正在使用lodash,则有_.camelCase()函数。

_.camelCase('Foo Bar');
// → 'fooBar'

_.camelCase('--foo-bar--');
// → 'fooBar'

_.camelCase('__FOO_BAR__');
// → 'fooBar'

我知道这是一个老答案,但这处理空格和_ (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"