sarathonline.com/blog | Trouble viewing this page? Give Feedback .
Try my blog on: Ajax Version
Search

Friday, October 03, 2008

Javascript RegEx missing group feature

I was looking for examples in Javascript to get grouped regular expressions into an array. Say a time string which I am collecting hour, min, and sec with a regular expression. I could do it in perl in a easy way, Something like this:

$_ = "10:23:25"
if (/(..):(..):(..)/) {
 @t = ($1, $2, $3);
}
But in javascript looks like there is no such way. the only way you could collect that is using the String.replace() and running the RE on it. Something like this.
str = "10:23:25"
re = /(..):(..):(..)/
hr = str.replace(re,"$1");
min = str.replace(re,"$2");
sec = str.replace(re,"$3");
t = new Array(hr, min, sec);
If you know of some other way, let me know :)

1 comments:

  1. Update: can be done, Look here.. http://blog.sarathonline.com/2009/01/some-time-back-i-was-comparing-perl-and.html

    ReplyDelete

© SarathOnline.com 2000-'11