加密號碼將中間四位改爲*

在實習的項目中,發現要對用戶提交的手機號碼進行加密顯示,百度後,總結了以下四種方法(參考):

  • 利用數組splice,split,join方法
      var tel = 18810399133
      tel = '' + tel
      var ary = tel.split('')
      ary.splice(3, 4, '****')
      var tel1 = ary.join('')
      console.log(tel1) // 188****9133
  • 利用字符串的substr方法
      var tel = 18810399133
      tel = '' + tel
      var tel1 = tel.substr(0, 3) + '****' + tel.substr(7)
      console.log(tel1) // 188****9133
  • 利用字符串substring方法
      var tel = 18810399133
      tel = '' + tel
      var tel1 = tel.replace(tel.substring(3, 7), '****')
      console.log(tel1) // 188****9133
  • 利用正則(推薦)
      var tel = 18810399133
      tel = '' + tel
      var reg = /(\d{3})\d{4}(\d{4})/
      var tel1 = tel.replace(reg, '$1****$2')
      console.log(tel1)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章