如何将js驼峰式命名(带有数字)转换为下划线命名

电脑杂谈  发布时间:2020-06-13 04:30:17  来源:网络整理

js下划线转换驼峰_转换成驼峰法的函数_c定义函数角度弧度转换

由于mysql数据表的命名,我需要在开发项目时将驼峰式案例名称转换为下划线名称转换成驼峰法的函数,类似于将userName1转换为user_name_1. 在我编写函数之后,可以转换某些属性,并且某些属性会出现问题. ,请诸神纠正我!

c定义函数角度弧度转换_转换成驼峰法的函数_js下划线转换驼峰

我使用正则表达式查询大写字母和数字,并在查询字符之前添加“ _”

转换成驼峰法的函数_c定义函数角度弧度转换_js下划线转换驼峰

//请粘贴下面的代码文本(不要用图片替换代码)

转换成驼峰法的函数_c定义函数角度弧度转换_js下划线转换驼峰

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <script>
    var info = {
      id: 1,
      id1: 2,
      userName1: '刘玄德',
      userName2: '刘玄德',
      userName3: '大哥',
      userAge: 45,
      userAge1: 46,
      userAge2: 47
    }
    var info2 = {
      id: 1,
      id_1: 2,
      user_name_1: '刘玄德',
      user_name_2: '刘玄德',
      user_name_3: '刘玄德',
      user_age: 45,
      user_age_1: 46,
      user_age_2: 47
    }
    var changeObj = function (obj) {
      var regNum = new RegExp(/\d/)
      var regStr = new RegExp(/[A-Z]/g)
      var arr = Object.keys(obj);
      arr.forEach((k, index) => {
        console.log('=====打印k=========')
        // console.log(k)
        // debugger
        
        if (!regNum.test(k) && !regStr.test(k)) {//既无数字,也无大写
          console.log("既无数字,也无大写")
          console.log(k)
        }else if( regStr.test(k) ){//有大写
          console.log("检测到大写字母")
          var newK3 = k.replace(/([A-Z])/g, "_$1").toLowerCase();
          console.log(newK3)
          if(regNum.test(newK3)){
            console.log("检测到数字")
            newK4 = newK3.replace(/([0-9])/, "_$1");
            console.log(newK4)
          }
        }else{//有数字
          console.log("检测到数字")
          var newK = k.replace(/([0-9])/, "_$1");
          console.log(newK)
        }
        
      })
    }
    changeObj(info)
  </script>
</body>
</html>

js下划线转换驼峰_c定义函数角度弧度转换_转换成驼峰法的函数

我想将信息转换为info2形式

var info = {
      id: 1,
      id1: 2,
      userName1: '刘玄德',
      userName2: '刘玄德',
      userName3: '大哥',
      userAge: 45,
      userAge1: 46,
      userAge2: 47
    }
    var info2 = {
      id: 1,
      id_1: 2,
      user_name_1: '刘玄德',
      user_name_2: '刘玄德',
      user_name_3: '刘玄德',
      user_age: 45,
      user_age_1: 46,
      user_age_2: 47
    }

但是我在控制台中看到的是userName_2无法检测到大写转换成驼峰法的函数,并且打印的信息如下:

驼峰.html:39 =====打印k=========
驼峰.html:44 既无数字,也无大写
驼峰.html:45 id
驼峰.html:39 =====打印k=========
驼峰.html:58 检测到数字
驼峰.html:60 id_1
驼峰.html:39 =====打印k=========
驼峰.html:47 检测到大写字母
驼峰.html:49 user_name1
驼峰.html:52 检测到数字
驼峰.html:54 user_name_1
驼峰.html:39 =====打印k=========
驼峰.html:58 检测到数字
驼峰.html:60 userName_2
驼峰.html:39 =====打印k=========
驼峰.html:47 检测到大写字母
驼峰.html:49 user_name3
驼峰.html:52 检测到数字
驼峰.html:54 user_name_3
驼峰.html:39 =====打印k=========
驼峰.html:44 既无数字,也无大写
驼峰.html:45 userAge
驼峰.html:39 =====打印k=========
驼峰.html:47 检测到大写字母
驼峰.html:49 user_age1
驼峰.html:52 检测到数字
驼峰.html:54 user_age_1
驼峰.html:39 =====打印k=========
驼峰.html:58 检测到数字
驼峰.html:60 userAge_2

众神可以请我更正我的代码并指出出了什么问题吗?以及如何纠正?


本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/jisuanjixue/article-243723-1.html

    相关阅读
      发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

      热点图片
      拼命载入中...