The method of transforming js hump naming (with numbers) into underlined naming

topic description

due to the naming of the mysql data table, I need to convert hump naming into underlined naming when developing the project, similar to userName1 to user_name_1 , after I write the function, some attributes can be converted, some properties will have problems, ask God to correct!

sources of topics and their own ideas

I use regular expressions to query capitals and numbers, and add "_"

before that character is queried.

related codes

/ / Please paste the code text below (do not replace the code with pictures)

<!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>

what result do you expect? What is the error message actually seen?

I want to convert info to 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
    }

problem description

but what I saw in the console cannot detect uppercase in userName_2. The print message is as follows:

.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

can you correct my code and point out what went wrong? And how to correct it?

Aug.02,2021

you can directly match uppercase and numbers at the same time / ([Amurz] |\ d +) / g
try to achieve your needs

const toLine = hump => hump.replace(/([A-Z]|\d+)/g, (a, l) => `_${l.toLowerCase()}`);

var info = {
  id: 1,
  id1: 2,
  userName1: '',
  userName2: '',
  userName3: '',
  userAge: 45,
  userAge1: 46,
  userAge2: 47
};

for (let k in info) {
  console.log(toLine(k))
}

<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
    }

    function copy(obj){//
      var newobj = {};
      for ( var attr in obj) {
          newobj[attr] = obj[attr];
      }
      return newobj;
    }
    var changeObj = function (obj) {
      var copyobj = copy(obj)
      var regNum = new RegExp(/\d/)
      var regStr = new RegExp(/[A-Z]/g)
      var arr = Object.keys(obj);

      var newObj = {}
      arr.forEach((k, index) => {
        var kArr = k.split('')
        var newK = ''
        kArr.forEach((item,index)=>{
          if(regNum.test(item)){
            kArr[index] = "_" + item
          }
          if(regStr.test(item)){
            kArr[index] = "_" + item.toLowerCase()
          }
        })
        newKey = kArr.join('')
        newObj[newKey] = obj[k]
        console.log(newKey)
        
      })
      console.log(newObj)
      return newObj
    }
    changeObj(info)
  </script>

Menu