Home > Development, Documentation, English, SCORM > JavaScript preg_match

JavaScript preg_match

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}H\d{2}M\d{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!

  1. September 29, 2010 at 4:09 am

    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:


    The spec: P[yY][mM][dD][T[hH][nM][s[.s]S]]

    There’s a useful thread on this topic 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.


  2. Yannick Warnier
    September 29, 2010 at 4:52 am

    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.

  3. September 29, 2010 at 12:37 pm

    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.

    • Yannick Warnier
      September 29, 2010 at 5:41 pm

      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.

  4. September 29, 2010 at 12:40 pm

    Alternatively, you can download the Ecmascript spec, 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 ;)

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: