1

I am having date string in following form x.toLocaleString().toLowerCase() "fri apr 21 2017 19:18:21 gmt+0530 (india standard time)" want to extract only 19:18:21 .How do extract these value only from whole string using javascript regular expression.

0

2 Answers 2

3

The REGEX should filter out invalid time formats like "60:23:22" which does not belong to either 12H or 24H formats, So it should be

var regExp = /([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]/g; 

var str = "19:18:21 65:45:33 12:23:23 30:34:22 100:22:22 10:22:222";
var regExp = /\b([01]\d|2[0-3]):[0-5]\d:[0-5]\d\b/g;
console.log(str.match(regExp));

Sign up to request clarification or add additional context in comments.

Comments

1

Use regular expression to extract the time with grouping:

var str = "fri apr 21 2017 19:18:21 gmt+0530 (india standard time)";

console.log(str.match(/(\d+:\d+:\d+)+/)[0])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.