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 :


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:


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.

Random recommended