[Javascript] Change Extensions

Shawn Milochik shawn at milochik.com
Wed Aug 31 07:52:46 CDT 2005


Falls, Travis D (HTSC, CASD) wrote:

>Hey Shawn,
>Another programmer on this list sent in an example that expanded on your
>file.extention reg exp example.  I delete his example by mistake... can you
>send that over to me... once I finish combining the two I will send back the
>reg exp so everyone on the list can have it.  thanks.
>  
>
Travis,

I hope this is what you were looking for.

Shawn

---------------------------
Paul Novitski wrote:
---------------------------

Shawn,

I'd make a couple of minor amendments to your regular expression:

        /^.*\.(.*)$/

First, this:

        /^.+\.(.+)$/

...replacing the asterisks ("0 or more instances") with pluses ("1 or 
more").  Without this change, I think your pattern would test true on 
all of these strings:
        file.ext
        file.
        .ext
        .
What would happen in the replace operation if the file name or its 
extension had zero length?

Also, I'd change it to accommodate querystrings:

        http://something.net/folder/file.ext?arg=val

        /^.+\.(.+)($|\?)/

...terminating at either end-of-string or a question mark.

Also, do you think we need to worry about period characters in the 
querystring?  I know they wouldn't be there in any properly URLencoded 
URL, but if they were typed there by a forgetful human then this might 
work:

        /^.+\.([^?]+)($|\?)/

replacing (.+) with ([^\?]+) meaning "any characters except the question 
mark."  (I recall that characters in a [list] don't need to be escaped 
with \.)

I'm not totally confident in greedy matching, so I'd also exclude the 
period from that:

        /^.+\.([^?.]+)($|\?)/

How'm I doing?

Paul




More information about the Javascript mailing list