命名更改小结:下划线转驼峰,或驼峰转下划线
程序员文章站
2022-06-01 07:50:35
...
1、下划线转驼峰
const toCamel = str =>str.replace(/([^_])(?:_+([^_]))/g, (_,p1, p2)=>p1+p2.toUpperCase());
2、驼峰转下划线
const toLowerLine = str =>str.replace(/[A-Z]/g, match=>"_"+match.toLowerCase());
3、打印结果:
console.log(toCamel("__to_make__something_wonderfull_"));
console.log(toLowerLine("whatIsYourName"));
附一张图来解释正则