ziwei3749 / blog

已停止更新..转移至 https://segmentfault.com/u/ziwei3749

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

正则练习

ziwei3749 opened this issue · comments

正则练习

题目1: 把类似于 is_good 这样的变量名替换成 isGood

原题: http://scriptoj.mangojuice.top/problems/29

       const toCamelCaseVar = (str) => {
            return str.replace(/([^_])_+([^_])/g,(match,$1,$2,index,str) => {
                console.log(match)
                console.log($1)
                console.log($2)
                return $1+$2.toUpperCase()
            })
        }

注意点:

  • [^_] 匹配除了_之外的任意字符
  • replace几个参数的含义,
    • match : 表示匹配到了什么?
    • $1 : 表示第1个括号匹配的
    • $2 : 表示第2个括号匹配的
    • index : 下标
    • str : 原来的字符串