简体   繁体   English

JavaScript每次出现时都会拆分一个字符串

[英]JavaScript split a string at each occurrence of

JavaScript, How can we split... JavaScript,我们如何拆分...

Input String: 12:002:204:457:109:40 输入字符串: 12:002:204:457:109:40

Output: 输出:

12:00
2:20
4:45
7:10
9:40

Use String#match method with regex /\\d{1,2}:\\d{2}/g . 对正则表达式/\\d{1,2}:\\d{2}/g使用String#match方法。

 console.log( '12:002:204:457:109:40'.match(/\\d{1,2}:\\d{2}/g) ) 


Regex explanation here. 正则表达式的解释在这里。

正则表达式可视化

Using regex: 使用正则表达式:

 '12:002:204:457:109:40'.match(/(\\d+:\\d{2})/g).map(x => console.log(x)) 

(\d+:\d{2})

正则表达式可视化

Debuggex Demo Debuggex演示

Further explanation: https://regex101.com/r/vV2wL5/1 进一步说明: https//regex101.com/r/vV2wL5/1

Try this.It is giving the exact O/P as per your need.I have tested it using alert(). 尝试一下,它会根据您的需要提供确切的O / P。我已经使用alert()测试了它。 Change it accordingly. 相应地更改它。

function aDarnal() {
  var s="12:002:204:457:109:40";
  var len= s.length;
   while(len >1)
   {
    var n = s.indexOf(":")+ 2;
    var x=  s.substr(0,n+1);
    alert(x);
    s=s.substr(n+1);
    len= s.length
   }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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