简体   繁体   English

我无法使这个简单的正则表达式正常工作

[英]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

  1. You can use \\d as a shorthand for [0-9] 您可以将\\d用作[0-9]的简写
  2. If you know how many digits it's supposed to have, use {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.

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