我应该使用什么RegEx来根据2个字格式和空格分割字符串? [英] What RegEx should I use to split a string according to 2 word format and spaces?

查看:76
本文介绍了我应该使用什么RegEx来根据2个字格式和空格分割字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试拆分字符串:

I am trying to split the string:


Chan 0: 2.50 0.12 13.92 83.46,Chan 1: 2.58 85.92 2.47 9.03,Chan 2: 5.00 85.0 33.33 6.64,Chan 3: 0.00 0.00 14.41 85.59

我试图得到的数字。我试图使用以下正则表达式:

I am trying to get just the numbers. I have tried to use the following regex:

/(\bChan\s[0-9]\b|\b,Chan\s[0-9]\b):\s|\s/

根据regextester.com我应该期待16个字符串,但是当我尝试打印包含分割字符串的var的长度时,我得到41个。

according to regextester.com I should expect 16 string but I am getting 41 when I try to print the length of the var that holds the split string.

我的代码是:

var x = values.value;
var v = x.split(/(\bChan\s[0-9]\b|\b,Chan\s[0-9]\b):\s|\s+/);
console.log(v.length);

其他问题:我尝试省略 | \s 最后将字符串拆分为 chan#字符串。当我尝试打印它时它确实分裂了我得到了9的长度我仍然可以打印我使用的分隔符,陈#:我认为返回的值是split只是我使用的分隔符之间的值而不是分隔符本身?

Additional question: I tried omitting the |\s at the end to split the string at the chan # string. It does split it I get a length of 9 when I try to print them I am stil able to print the delimiter I used, Chan #: I thought that the returned value of split is just the values in between the delimiter I used and not the delimiter itself?

推荐答案

你可以用一个数字来提取数字点亮它。

You could just extract the numbers with a dot in it.

var string = "Chan 0: 2.50 0.12 13.92 83.46,Chan 1: 2.58 85.92 2.47 9.03,Chan 2: 5.00 85.0 33.33 6.64,Chan 3: 0.00 0.00 14.41 85.59",
    values = string.match(/\d+\.\d+/g); 

console.log(values.length);
console.log(values);

这篇关于我应该使用什么RegEx来根据2个字格式和空格分割字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆