简体   繁体   中英

How can I split a large number to 3 groups of numbers using JavaScript?

I have a serial like this 12k4353535352311 and I want to make it like below using JavaScript:

12k4-35353535-2311

I have tried this, but I think it's useless

$('#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]; 
});
<script>
var key = "12k4353535352311";
var res = key.substring(0,4)+'-'+key.substring(4,12)+'-'+key.substring(12,16); //This contains 12k4-35353535-2311
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM