[英]I can't get this simple regex to work correctly
I'm doing this in Javascript 我正在用Javascript做
Here's my string: 09.22.2011-23.21-west-day
这是我的字符串:
09.22.2011-23.21-west-day
Here's what I'd like to pull out of it: 23.21
这是我想退出的内容:
23.21
Here's my expression: /-[0-9]*\\.[0-9]*/
这是我的表情:/-[
/-[0-9]*\\.[0-9]*/
I get -23.21
so I'd have to run aa replace method after this to remove the "-". 我得到
-23.21
因此必须在此之后运行aa replace方法以删除“-”。 Is there a way to leave out the "-" with this regex or not? 是否可以通过此正则表达式删除“-”? I never really perfected my regex skills because I usually use PHP which has the nice () capture groups.
我从来没有真正完善过我的正则表达式技能,因为我通常使用PHP,它具有不错的()捕获组。
Javascript has capture groups too: Javascript也具有捕获组 :
var time = "09.22.2011-23.21-west-day".match(/-(\d{2}\.\d{2})/)[1]
Gives you 23.21
给你
23.21
\\d
as a shorthand for [0-9]
\\d
用作[0-9]
的简写 {n}
or {m,n}
quantifiers. {n}
或{m,n}
量词。 It will reduce the chance of it matching the wrong thing. You can use ()
in JavaScript too! 您也可以在JavaScript中使用
()
!
s = '09.22.2011-23.21-west-day';
rx = s.match('-([0-9]*\.[0-9]*)')
alert(rx[1]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.