在包含两者的字符串中拆分数字和字母 [英] Splitting numbers and letters in string which contains both

查看:79
本文介绍了在包含两者的字符串中拆分数字和字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试拆分以下(或类似)字符串08-27-2015 07:25:00 AM。
目前我使用

I am trying to split following (or similar) string "08-27-2015 07:25:00AM". Currently I use

var parts = date.split(/[^0-9a-zA-Z]+/g);

导致

["02", "27", "2012", "03", "25", "00AM"]

问题在于 00AM 部分。我希望它也能分开。所以完美的结果将是:

The problem is with the 00AM part. I want it to be separated too. So the perfect result would be:

["02", "27", "2012", "03", "25", "00", "AM"]


推荐答案

如果你正在寻找字母或数字的序列,但不是混合的序列,你可以这样做...

If you're looking for sequences of letters or numbers, but not a sequence both mixed, you can do this...

"08-27-2015 07:25:00AM".match(/[a-zA-Z]+|[0-9]+/g)

导致......

["08", "27", "2015", "07", "25", "00", "AM"]

开在 | 的任一侧,我们有一个或多个字母的序列和一个或多个数字的序列。因此,当它出现一封信时,它会收集所有连续的字母,直到它达到非字母,此时它会收集所有连续的数字,依此类推。

On either side of the |, we have a sequence of one or more letters and a sequence of one or more numbers. So when it comes across a letter, it will gather all contiguous letters until it reaches a non-letter, at which point it gathers all contiguous numbers, and so on.

任何其他字符都不匹配,因此它不会成为结果的一部分。

Any other character simply doesn't match so it doesn't become part of the result.

这篇关于在包含两者的字符串中拆分数字和字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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