简体   繁体   English

正则表达式javascript数字和字母

[英]regular expression javascript numbers and letters

I can't for the life of me seem to figure out how to set a regular expression of an element with the formatting. 我不能为我的生活似乎弄清楚如何使用格式设置元素的正则表达式。

1d 5h 6m 12s

And also allow it to have any variation of those such as 并允许它有任何变化,如

1d 

1d 1h

1d 1h 1m 

1d 1s

6m 12s 

etc...

Is it possible with regular expressions to do that tyle of formatting? 是否可以使用正则表达式来实现格式化?

Assuming you need it to be order, 假设您需要订购,

if (/^\s*(?:(?:[1-9]\d*|0)d\s+)?(?:(?:1?\d|2[0-3])h\s+)?(?:[1-5]?\dm\s+)?(?:[1-5]?\ds)?\s*$/.test(str))
{
    // success
}

Here's a quick breakdown: 这是一个快速细分:

  1. The ^ and $ are known as anchors . ^$被称为锚点 They match the beginning and end of a string, so you're matching the entire string, not only a part, eg hello, world! 1d 5h 6m 12s 它们匹配字符串的开头和结尾,因此您匹配整个字符串,而不仅仅是一个部分, 例如 hello, world! 1d 5h 6m 12s hello, world! 1d 5h 6m 12s would pass otherwise. hello, world! 1d 5h 6m 12s否则通过。

  2. The \\s* and \\s+ match zero or more, and one or more, whitespace characters. \\s*\\s+匹配零个或多个,以及一个或多个空格字符。

  3. The (?:[1-9]\\d*|0) matches an arbitrary number of digits but not one that start with zero, unless it's exactly zero. (?:[1-9]\\d*|0)匹配任意数量的数字,但不匹配以零开头的数字,除非它正好为零。

  4. The (?:1?\\d|2[0-3]) matches the digits between 0 and 23, inclusive. (?:1?\\d|2[0-3])匹配0到23之间的数字,包括0和23。

  5. The [1-5]?\\d matches the digits between 0 and 59, inclusive. [1-5]?\\d匹配0到59之间的数字,包括0和59。

  6. The (?: ... ) are known as non-capturing groups . (?: ... )被称为非捕获组 They're like parentheses (for grouping) except that plain ol' parentheses capture , and we don't need that here. 它们就像括号(用于分组),除了普通的'括号' 捕获 ,我们在这里不需要它。

  7. The ? ? means the preceding entity is optional. 表示前一个实体是可选的。

To give you a starting point: 给你一个起点:

(\d+d){0,1} //days - not checking for a max

(((([0-1]){0,1}\d)|(2[0-4]))h){0,1} // 24 hours

(((([0-5]){0,1}[0-9])|(60))m){0,1} //60 minutes

(((([0-5]){0,1}[0-9])|(60))s){0,1} //60 seconds

Then put them all together (in this case not worrying about amount of whitespace) 然后把它们放在一起(在这种情况下不要担心空白量)

(\d+d){0,1}[ ]*(((([0-1]){0,1}\d)|(2[0-4]))h){0,1}[ ]*(((([0-5]){0,1}[0-9])|(60))m){0,1}[ ]*(((([0-5]){0,1}[0-9])|(60))s){0,1}[ ]*

Including @nhahtdh 's improved version of the above from the comments. 从评论中包括@nhahtdh的上述改进版本。 Thanks! 谢谢!

((\d+)d)? *(([01]?\d|2[0-4])h)? *(([0-5]?\d|60)m)? *(([0-5]?\d|60)s)? *

I think this is what you want: 我想这就是你想要的:

function parse(s) {
 y = s.match(/(?:(\d+)d\s*)?(?:(\d+)h\s*)?(?:(\d+)m\s*)?(?:(\d+)s)?/);
 console.log(y);
 return y;
}

Here's how it works: 以下是它的工作原理:

  • (\\d+)d\\s* matches some digits followed by a d , followed by optional whitespace (\\d+)d\\s*匹配一些数字后跟一个d ,后跟可选的空格
  • wrapping it in (?:...)? 将它包裹在(?:...)? , like (?:(\\d+)d\\s*)? ,像(?:(\\d+)d\\s*)? makes the above optional. 使上面的选项。 The ?: causes the new set of parentheses to not be a capturing group. ?:导致新的括号组不是捕获组。
  • repeat for h, m and s and you're done. 重复h,m和s,你就完成了。

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

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