下划线与驼峰相互转化?

相互转化首先想到的事用正则进行匹配

1、首先是下划线转驼峰

1 const lineToHump = (name = '') => {
2    return name.replace(/\_(\w)/g, (all, letter) => {
3       return letter.toUpperCase();
4    });
5 };

  console.log(lineToHump('a1_b2_c3_d4_f5'));
   //a1B2C3D4F5

2、其次驼峰转下划线

1 const humpToLine = (name = '') => {
2    return name.replace(/([A-Z])/g, "_$1").toLowerCase();
3 };

  console.log(humpToLine('aB2345C2345'));
 //a_b2345_c2345

 

posted @ 2020-08-12 10:11  程序員劝退师  阅读(1917)  评论(0编辑  收藏  举报