1、每隔4位添加 -
var data = "1254856332111122klklhh"; var result = ""; for(let i = 0; i < data.length; i++) { if (i % 4 == 0 && i != 0) { result += '-' + data[i]; } else { result += data[i]; } } console.log(result);// 1254-8563-3211-1122-klkl-hh
2、每隔2位添加 ,
var data = "1254856332111122klklhh"; var result = ""; for (let i = 0;i < data.length; i++ ){ result += data[i]; if(i % 2 === 1) result += ','; } console.log(result); // 12,54,85,63,32,11,11,22,kl,kl,hh,