#29 转换驼峰命名


  • 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;
    };

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


  • 0

    const toCamelCaseVar = (variable) => {
       return variable.replace(/([^_])_+([^_])/g, function (all, b, c) {
            return b+c.toUpperCase()
        })
    }
    

  • 0

    @一个男子汉 你这没通过测试呀


  • 0

    const toCamelCaseVar = (variable) => {
        variable = variable.trim();
        let arr=variable.split('');
        let start = 0;
        let end = arr.length-1;
        let countEnd = 0;
        while(arr[start]==='_'){start++};
        while(arr[end]==='_'){end--;countEnd++}
        arr = [...arr.slice(start,end+1)];
        for(let i = 0; i<arr.length; i++) {
            if(arr[i]==='_') {
              let k = 0;
              while(arr[i+k+1]==='_') {k++;}
              arr = [...arr.slice(0,i),arr[i+1+k].toUpperCase(),...arr.slice(i+2+k,end+1)];
            }
            
        }
        let result =  arr.join('');
        result=result.padStart(result.length+start,'_');
        result=result.padEnd(countEnd+result.length,'_');
        return result;
      
    }
    
    

  • 0

    const toCamelCaseVar = (variable) => {
      return variable.replace(/_+[^_]/g, (...arg) => {
        if (arg[1]) {
          let rt = arg[0];
          return rt[rt.length - 1].toUpperCase();
        } else 
          return arg[0];
      });
    }
    

  • 0

    @Edge 0_1542770664009_upload-64d94c01-114c-4c68-87ca-b31db928baf3
    我也是提交报错


  • 0

    const toCamelCaseVar = (variable) => variable.replace(/([^_])(?:_+([^_]))/g, (match, p1, p2) => p1 + p2.toUpperCase())


  • 0

    一种方法:

    const toCamelCaseVar = (variable) = > {
      const reg = /^_*/;
      // 匹配字符串开头的_
      const start = variable.match(reg);
    
      // 两步替换,第一步将开头的_消除
      // 第二步将其余的_消除,后面紧跟的字符放在捕获组p1中,转换为大写
      let result = variable.replace(reg, '').replace(/_+(.)/g, (match, p1) = > p1.toUpperCase());
      
      // 如果之前消除过开头的_,需要加回来
      if (start && start.length > 0) {
        result = start[0] + result;
      }
      return result;
    };
    

    另一种方法:

    const toCamelCaseVar = (variable) => variable.replace(/([^_])(_+)(.)/g, (match, p1, p2, p3) => p1 + p3.toUpperCase());
    

  • 0

    const toCamelCaseVar = (variable) => variable.replace(/_+[a-zA-Z]/g,(m, i) => { return i ? (m.match(/[a-zA-Z]/)[0].toUpperCase()) : m})


  • 0

    @maroon 两个this


  • 0

    const toCamelCaseVar = (variable) => variable.replace(/_+[a-zA-Z]+?/g, (word, i) => i == 0 ? word : word.substring(word.length-1).toUpperCase())
    

  • 0

    貌似ScriptOJ不支持反向预查语法 (?<=...),首页拓展名那题也是,都会报错

    Invalid regular expression: /(?<=[^^]\w)_(\w)/: Invalid group

    本地测试下面这行是ok的
    const toCamelCaseVar = (variable) => variable.replace(/(?<=[^^]\w)\_(\w)/g, (p1) => p1.toUpperCase());


  • 0

    @沉默的张大爷 js 只支持正向预查, 你在 node 环境下跑的(V8)?


登录后回复
 

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