Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 298
  • Last Modified:

REGEX of a HTML to get Hours

Hi all

I have the following string:
08:00<BR><DIV style="COLOR: red">17:00</DIV>

Open in new window


I can receive any hours indicated in the string.

Is there a way of getting an array of "hours" by using Regular Expressions?

I mean something like
var from = "08:00<BR><DIV style="COLOR: red">17:00</DIV>";
var to = from.match("fill_with_regular_expression");
// to[0] = "08:00"
// to[1] = "17:00"

Open in new window


in advance, many many thanks!

0
Gabriel_Espinoza
Asked:
Gabriel_Espinoza
  • 3
  • 3
  • 2
2 Solutions
 
mrjoltcolaCommented:
You an use the global match modifier to return an array of matches.

>> var all = from.match("fill_with_regular_expression", "g");

Number of matches will be all.length


http://freewebdesigntutorials.com/javaScriptTutorials/jsRegularExpressionObject/regExpGlobalProperty.htm
0
 
Gabriel_EspinozaAuthor Commented:
Thank you for the fast reply,

I know I should use the "g" to list all matched elements. It was my bad for not pointing it out.

Now, could you help me with the regular expression?

0
 
mrjoltcolaCommented:
Something like this:

  (\d\d:\d\d)

or

  (\d{2}):(\d{2})
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
käµfm³d 👽Commented:

var to = from.match(/\d{1,2}:\d{1,2}/g)

Open in new window

0
 
Gabriel_EspinozaAuthor Commented:
thank you all for the fast reply.

I tried all the expression you posted, none of them returned anything. I'm still getting a null.

Any more ideas?
0
 
käµfm³d 👽Commented:
I tried all the expression you posted, none of them returned anything. I'm still getting a null.
Funny. Mine is not null.

<html>
	<head>
		<script type="text/javascript">
			var from = "08:00<BR><DIV style=\"COLOR: red\">17:00</DIV>";
			var to = from.match(/\d{1,2}:\d{1,2}/g);

			alert(to);
		</script>
	</head>
	<body>
	</body>
</html>

Open in new window

untitled.PNG
0
 
Gabriel_EspinozaAuthor Commented:
thank you very much, it was my mistake cause I was using cuotes to pass the regex as parameter.

Thank you very much
0
 
käµfm³d 👽Commented:
NP. Glad to help  : )
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now