截取字符串到指定长度数组

var str = "H66S2RY16kmSPUdIWApAOc2T5piLpw77lUQU93kky5WtqaJcd81YBx5FFhps0eSkwpFmDVMQQeJTrH5YlPjAbj2mNlZYrIagKvJoasU0WfY7vyHXchNb50iorsTFJkPr43vvDAEK2V6t2budSykUMGnYL4GjUO8maCqJN4oBWnhuP15zEEG0u68sUrdXOPNIdwCDEEn8"

// 截取 20 个为一组
var strArray = str.split(/(.{20})/).filter(s => s);

结果:

["H66S2RY16kmSPUdIWApAOc2T5piLpw77lUQU93kk", "y5WtqaJcd81YBx5FFhps0eSkwpFmDVMQQeJTrH5Y", "lPjAbj2mNlZYrIagKvJoasU0WfY7vyHXchNb50io", "rsTFJkPr43vvDAEK2V6t2budSykUMGnYL4GjUO8m", "aCqJN4oBWnhuP15zEEG0u68sUrdXOPNIdwCDEEn8"]

说明

["a","b",""].filter(s => s)

相当于

["a","b",""].filter(function(one) {
  if (one) { // 如果这个值不为空
    return true;
  } else {
    return false;
  }
})
// 结果是 ["a", "b"]

推荐阅读更多精彩内容