In the context of a nasty (wrong) SCORM package, I had to find a method of quickly parsing a time representation like "PT03H23M34S" and transform it into "03:23:34".
After a few quick searches on how to do user string.replace(), I had to change the method, because apparently replace() doesn't allow for selection identifiers (2 and the likes) in the replacement string, or at least I didn't find how to do it.
So I ended up with this:
if (/[A-Z]{2}d{2}Hd{2}Md{2}S/i.test(param)) { param = param.split(/D+/).join(' ').trim().split(/S/).join(':');}It *is* kind of a one-liner. Not very beautiful but it seems to do the trick. On my way finding the solution, I had the opportunity to play a little with the JavaScript console of Firebug. Nice!
Comments
Hi Yannick
I was recently digging into this topic as well. I like the simplicity of your solution, but unfortunately, it will only work if the string strictly follows the example in your post.
Here are some timeinterval strings that I've been using in my testing; they're all valid according to the SCORM 2004 spec, but fail with the RegEx you posted:
PY3M2DT3H
P1Y3M2DT3H
P1Y3M2D
PT3H5M
PT05H
PT5H
PT000005H
PT00S
P0Y3M2DT3H
PYMDTH23M15S
PYMDT2HM15.23S
P1Y
PT0S
The spec: P[yY][mM][dD][T[hH][nM][s[.s]S]]
There's a <a href="http://stackoverflow.com/questions/1307347/scorm-2004-time-format-regul…; rel="nofollow">useful thread on this topic</a> at Stack Overflow. None of the RegEx patterns there are as elegant as yours, but they manage to handle most of the oddities of the SCORM 2004 timeinterval format.
What makes this so tricky (in my opinion) is that the content/SCO developer is the person who writes the timeinterval string. The spec provides so much flexibility that parsing the string becomes much more complex than it should be.
Cheers!
Wow, great comment Philip!
Actually to me the format was completely wrong because I was looking into SCORM 1.2 (which is what the content is supposed to be in this case), and in this version the only format allowed is 0000:00:00.00, where the two first and the two last zeros are optional.
I wonder why they added that much flexibility in SCORM 2004, given for session_time in SCORM 1.2 you just have to add the time to the total_time. I guess that's been extended in SCORM 2004 as well. But honestly... who's going to follow a SCORM for more than a day!? (these guys writing the specs have no common sense ;-) I hope you're not part of them, lol - if you are do not hesitate to explain).
Well, in my case it was just a quick fix for a badly made SCORM 1.2 content. Sorry it couldn't cover your need.
Note that the RegExp.exec() function might work with a little more effort than what I've been giving it. In PHP or Perl, the whole RegExp would surely be possible in a one-liner.
Thanks for sharing.
Javascript/Ecmascript uses dollar, not backslash to refer to captured submatch groups in replacement text.
I can recommend the excellent book Javascript: The Definitive guide by Flanagan, which contains a great reference section in which you can find this kind of info in a matter of seconds.
Yeah, I have the book... in Belgium (currently far far away) :-)
Thanks for the comment, I think I tried it and it didn't work. Must have done something wrong.
Alternatively, you can download the <a href="http://www.ecma-international.org/publications/standards/Ecma-262.htm&q…; rel="nofollow">Ecmascript spec</a>, which contains formal descriptions of all the Ecmascript standard functions and language constructs. It doesn't contain Javascript extensions nor does it contain a reference on the DOM classes. It's also very formal so it's not exactly the best reference guide. But hey, at least it's free of charge ;)