Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the following sting "
11:50:00
how can I match minutes which is 50?

What I have tried:

var minutes = str.match(/\:/d{2}\:/);//matches ":50:", how can I match only the number?
Edited:
I have already found out how to match minutes with this regex arg.match(/\d{2}(?=:\d+$)/);
The problem is, if a user types a space before it, this still will match, how can I prevent it?
Posted
Updated 1-Aug-16 1:02am
v3
Comments
Karthik_Mahalingam 1-Aug-16 6:01am    
var minutes = str.split(':')[1];
AlexLearne 1-Aug-16 6:10am    
Thank you for your answer, but regex is really preferable here since I would like to check if somebody typed 2 numbers and also string is not acceptable. I would need to perform some calculations on minutes later. But your solution really works)

Try:
(?<=\d+:)\d{2}(?=:\d+)

[edit]
Stupid language Javascript - it's regular expressions don't support lookbehind... :sigh:

Try this:
\d{2}(?=:\d+$)
It's not as robust, but it'll work in JS...
[/edit]
 
Share this answer
 
v2
Comments
AlexLearne 1-Aug-16 6:01am    
I am sorry, it gives me a syntax error, should it be like this?
str.match(/(?<=\d+:)\d{2}(?=:\d+)/) ?
OriginalGriff 1-Aug-16 6:17am    
Answer updated.
Patrice T 1-Aug-16 6:24am    
Looks the correction is to remove the "<"
My mistake
OriginalGriff 1-Aug-16 6:59am    
I forgot JS doesn't support regexes properly as well! :laugh:
AlexLearne 1-Aug-16 6:20am    
You are a lifesaver, thanks!
Tools to help build and check RegEx
Expresso Regular Expression Tool[^]
.NET Regex Tester - Regex Storm[^]
and this one that also show your RegEx as a nice graph:
Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900