正则相关

247 阅读1分钟
  1. 先行断言 后行断言 匹配happy-new-year的-后第一个字母

    1. str.match(/(?<=[-])(\w)/)

    2. str.replace(/(?<=[-])(\w)/,(x)=>{return x.toUpperCase()}) replace第二个参数,接受↓

    str.replace(/(?<=[-])(\w)/g,(x,y,z,k)=>{console.log(x,y,z,k)})
    结果:
     n n 6 happy-new-year
     y y 10 happy-new-year
     匹配到的结果 第一个括号里的内容 结果的index 完整的内容
    "happy-undefinedew-undefinedear"