#29 转换驼峰命名


  • 0

    variable.replace(/(^_+)|(_+\S)/g, (match) => match[match.length-1] === '_' ? match : match[match.length-1].toUpperCase())
    

  • 0

    @CodeHz 感觉匹配尾部的下划线会有问题,举个栗子:

    const toCamelCaseVar = variable => variable.replace(/([^_])(?:_+(\S))/g, (_, f, p) => f + p.toUpperCase())
    const varName = '__aaa__bbb_ccc__';  // <- 尾部有两个下划线
    console.log(toCamelCaseVar(varName)); // __aaaBbbCcc_ <- 只剩下一个下划线
    

    改成如下的正则式是否会更合理一些?

    const toCamelCaseVar = variable => variable.replace(/([^_])(?:_+([^_]))/g, (_, f, p) => f + p.toUpperCase())
    const varName = '__aaa__bbb_ccc__';  // <- 尾部有两个下划线
    console.log(toCamelCaseVar(varName)); // __aaaBbbCcc__
    

  • 0

    遇见正则的就一脸懵逼了


  • 0

    处理 _$this_is_my_name 的返回结果不正确
    返回$thisIsMyName也不对吗?


  • 0
    administrators

    @裂章

    变量名首尾的下划线不需要做处理,中间的下划线全部删除并且处理成驼峰。


  • 0

    const toCamelCaseVar = (variable) => {
      let underlineStart = variable.match(/^_+/g) ? variable.match(/^_+/g)[0] : '';
      var output = variable.replace(/(_+)([a-z$A-Z]+)/g, (match, p1, p2) => {
        return p2.toUpperCase()[0] + p2.slice(1);
      })
      return underlineStart + output.toLowerCase()[0] + output.slice(1);
    }
    

    感觉自己写复杂了


  • 0

    const toCamelCaseVar = s => s.replace(/([^])+(.)/g, (_, p, n) => p + n.toUpperCase())


  • 0

    var toCamelCaseVar = function (str){
    return str.replace(/([^]+)+(\S)/g,function(match,$1,$2,string){
    return $1+$2.toUpperCase()
    })
    }


  • 0

    比较长的一行代码

    const toCamelCaseVar = (v) => (/^_+/.exec(v) ? /^_+/.exec(v)[0] : '') + v.replace(/(^_+)|(_+$)/g,'').replace(/_+[\w$]/g,x=>x[x.length-1].toUpperCase())+ (/_+$/.exec(v) ? /_+$/.exec(v)[0] : '')
    

  • 0

    ‘this_is_my_name’在控制台输出'thisIsMyName',但是提交报错了啊。怎么回事!
    0_1512446512822_upload-0b6babe9-59a4-40c3-9174-aa17080cb29a


  • 0

    简直不要太长

    const toCamelCaseVar = function(variable){
      return variable.replace(/[a-z]_+[a-z]/gi, function(s){ return s.replace(/\w$/gi, function(s){return s.toUpperCase()}).replace(/_+/gi, '') })
    }
    

  • 0

    // 感觉不是很优雅
    var toCamelCaseVar = (variable) => {
            var start = variable.match(/^_+/g) === null ? '' : variable.match(/^_+/g)[0];
            var end = variable.match(/_+$/g) === null ? '' : variable.match(/_+$/g)[0];
                var midStr = variable.replace(/^_+|_+$/g, '');
            var mid = midStr.replace(/_+([\d\D])/g, (all, match) => {
                return match.toUpperCase();
            });
                return start + mid + end;
            }
            toCamelCaseVar('_$this_is_my_name'); // _$thisIsMyName
    

  • 0

    var transferArr = []
    var num = []
    const toCamelCaseVar = variable => {
    transferArr = variable.split('')
    const arrNum = transferArr.length
    for (var i = 0; i < transferArr.length; i++) {
    if (transferArr[i] !== '') {
    num.push(i)
    }
    }
    if (transferArr[0] === '
    ') {
    transferArr = transferArr.splice(num[0])
    }

    if (transferArr[transferArr.length - 1] === '') {
    transferArr = transferArr.slice(0, num[num.length - 1] + 1)
    }
    if (transferArr.includes('
    ')) {
    for (var i = 0; i < transferArr.length; i++) {
    if (transferArr[i] === '') {
    transferArr[i + 1] = transferArr[i + 1].toUpperCase()
    }
    }
    }
    variable = transferArr.join('').replace(/
    /g, '')

    for (var i = 0; i < num[0]; i++) {
    variable = '_' + variable
    }

    for (var j = 0; j < arrNum - num[num.length - 1] - 1; j++) {
    variable = variable + '_'
    }
    return variable
    }

    显示“处理 _$this_is_my_name 的返回结果不正确” 控制台显示正确


  • 0

    @陈小俊
    执行这个测试:toCamelCaseVar('is$good_wrong')
    返回结果 :"is$goodWrong"


  • 0

    const toCamelCaseVar = (variable) => variable.replace(/([^_])(_+($)([^_]))/g,(...arg)=>arg[1]+arg[2].replace( /_/g,'').toUpperCase())
    通过测试的代码,求求优化


  • 0

    const toCamelCaseVar = (variable) => variable.replace(/(?:[^_])(\_+)([^_])/g,(a,b,c)=>a[0]+c.toUpperCase());

  • 0

    @ScriptOJ
    const toCamelCaseVar = (variable) => variable.replace(/(?<=[^])(_+)([^])/g,(a,b,c)=>c.toLocaleUpperCase());
    (?<=pattern)
    非获取匹配,反向肯定预查,与正向肯定预查类似,只是方向相反。
    不支持反向肯定预查。


  • 0

    @陈小俊 我运行了你的代码, __thisIs_myName这个的执行结果是 __thisIsMyName,题目说首位的_不做处理,中间的_是要处理的,那第二个‘下划线’算首?不用处理么?


  • 0

    const toCamelCaseVar = (variable) => variable.replace(/(?:[^_])(?:\_+)([^_])/g,(a,b)=>a[0]+b.toLocaleUpperCase());
    

    表示中间是_,两边非_。
    a为大匹配保留第一位,b为小匹配转换成大写。


  • 0

    const toCamelCaseVar = (variable) => {
    let Continue = true, str = variable, unq = "";
    while (Continue) {
    let index = str.indexOf("");
    if (index === -1 || index === str.length - 1) {
    Continue = false;
    } else if (index === 0) {
    str = str.substring(1);
    unq = unq + "
    ";
    } else {
    let str1 = str.substring(0, index),
    str2 = str.substring(index + 1),
    str3 = str2[0].toUpperCase() + str2.substring(1);
    str = str1 + str3;
    }
    }
    return unq + str;
    };

    还是通过了,新手,不会用正则表达式,只能写一大篇了


登录后回复
 

与 ScriptOJ 的连接断开,我们正在尝试重连,请耐心等待