current position:Home>Separate strings and insert a comma for each 5 characters

Separate strings and insert a comma for each 5 characters

2022-02-03 00:14:54 CSDN Q & A

A string with an indefinite length, such as ( Hour and minute of clock in time ):19:0619:0719:0808:0108:0308:04

You need to separate each clock in time with a comma , Because the number of clocks is uncertain , So the total length of the string is uncertain , The results are similar :

19:06,19:07,19:08,08:01,08:03,08:04




Refer to the answer 1:
 var str = '19:0619:0719:0808:0108:0308:04';     var b=str.replace(/(.{
   
   
   
   5})/g,"$1,");    console.log(b,b.split(",")); 



Refer to the answer 2:



Refer to the answer 3:

img

var str = "19:0619:0719:0808:0108:0308:04"var strArr = [];var n = 5for (var i = 0, l = str.length; i < l/n; i++) {    var a = str.slice(n*i, n*(i+1));    strArr.push(a);}var str1 = strArr.join(',')console.log(str1)



Refer to the answer 4:

copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.
https://en.primo.wiki/2022/02/202202030014531905.html

Random recommended