常见的正则面试题

1.匹配xxyy模式

var reg = /(\w)\1(\w)\2/g;

2.the-first-name 变成theFirstName

      var str = "the-first-name"
      var reg = /-(\w)/g
      str.replace(reg,($,$1)=>{
          return $1.toUpperCase()

      })

3.aaabbbbcccc变成abc

    var str = 'aaaaaaaaabbbbbbbbcccccc'
    var reg = /(\w)\1*/g

    console.log(str.replace(reg,'$1'))

4.100000000变成100.000.000

    var str = '10000000000'
    var reg = /(?=(\B)(\d{3})+$)/g
    str.replace(reg,'.')

你可能感兴趣的:(学习)