Flagrant Badassery

A JavaScript and regular expression centric blog

parseUri: Split URLs in ColdFusion

Update: I've added a JavaScript implementation of the following UDF. See parseUri: Split URLs in JavaScript.

Here's a UDF I wrote recently which allows me to show off my regex skillz. parseUri() splits any well-formed URI into its components (all are optional).

The core code is already very brief, but I could replace everything within the <cfloop> with one line of code if I didn't have to account for bugs in the reFind() function (tested in CF7). Note that all components are split with a single regex, using backreferences. My favorite part of this UDF is its robust support for splitting the directory path and filename (it supports directories with periods, and without a trailing backslash), which I haven't seen matched in other URI parsers.

Since the function returns a struct, you can do, e.g., parseUri(uri).anchor, etc. Check it out:

See the demo and get the source code.

There Are 10 Responses So Far. »

  1. very cool! thanks for sharing this.
    one scope issue: line 35 “Variables.uriParts.pos” should be “uriParts.pos”

  2. Found the same issue as hua… one suggestion, could you split the host part into subdomain and domain too?

  3. Simple, robust and powerful UDF! Exactly what I looking for!
    Great work!

  4. Thanks man!

  5. Thanks funny site incest art toplisty atdn

  6. We went to university together lolita girls in thongs >:[[[

  7. I live here Pthc Bbs Forum fyleul

  8. Could you tell me my balance, please? Nude Teen Models :-[[

  9. Could I order a new chequebook, please? Sarka Model Teen

  10. the anchor value is available if you create on the server the variable you are parsing. The incoming url from a user’s submitted request will not include the anchor – the behavior of browsers is to not send it. Suppose you progammatically want to highlight the point on the page indicated by the anchor? If you are in control of creating the page having the link the user will click, include the variable in the url:


    or alternately, if you have control of the response page, use javascript and have jQuery or onLoad() process the anchor:

    (supposing http://a.com#asdf and asdf is both the anchor in the link and, on the response page, the id value of the element you want to highlight and you have jQuery)

    <script type=”text/javascript” src=”/scripts/jquery-1.7.2.min.js”></script>
    <script type=”text/javascript”>
    with id:
    <div id=”asdf”>hello world</div>
    without id:
    <div>goodbye world</div>

Post a Response

If you are about to post code, please escape your HTML entities (&amp;, &gt;, &lt;).