如何使用JavaScript将大量数字拆分为3组数字?

时间:2015-11-11 12:04:02

标签: javascript jquery split

我有一个像12k4353535352311这样的序列号,我希望使用JavaScript来实现如下:

12k4-35353535-2311

我试过这个,但我觉得它没用了

$('#dev_serial').change(function(){
   //var serial = $('#dev_serial').val();
   var number =$('#dev_serial').val();
    output = [],
    sNumber = number.toString();
     alert(output);

/* Outputs:
 *
 * [1, 2, 3, 5, 4, 9, 8, 7]
 */ 
   //var series = (''+serial)[1]; 
});

1 个答案:

答案 0 :(得分:1)

<script>
var key = "12k4353535352311";
var res = key.substring(0,4)+'-'+key.substring(4,12)+'-'+key.substring(12,16); //This contains 12k4-35353535-2311
</script>