[Javascript] Change Extensions

Paul Novitski paul at novitskisoftware.com
Thu Aug 25 13:42:55 CDT 2005


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



At 10:46 AM 8/25/2005, Shawn Milo wrote:
>On 8/25/05, Falls, Travis D (HTSC, CASD) <Travis.Falls at thehartford.com> wrote:
> > I am trying to write a javascript method that will use regular expressions
> > to figure out the file extension of a file name (String) and change it.
> > Basically I need to find ".*" and replace it with ".working"  I don't know
> > how to write this regular expression though.  Can someone explain how to do
> > this? (not just send over the regular expression I want to learn how to do
> > this).  Thanks.
>
>
>Travis,
>
>You want a regex that says:
>"Find the last period, then remember everything after that."
>
>In regex syntax, a period is a reserved character, so you have to
>"escape" it with
>a backslash (so it would be \. instead of .).
>
>So you have:   /\./   (A regex is put inside forward slashes.)
>
>Now, you want to make sure it's the last period. Easy. The special
>character '.' (the period) matches any character. The asterisk (*)
>matches zero or more. So look for zero or more characters followed by
>a period.
>
>  /.*\./   (Note that due to "greedy matching," the .* will match any
>and all periods up to the last, so you don't have to worry about it
>stopping early.)
>
>Now, you want to find everything after the last period. Easy, another '.*'.
>
>   /.*\..*/
>
>Surround the part you want to replace with parenthesis.
>
>  /.*\.(.*)/
>
>Use good clean syntax, and specify the beginning of line (^) and end
>of line ($).
>
>  /^.*\.(.*)$/
>
>So if you want to remember what it was before you replace it, you can
>reference it like this:
>
>oldExt = = strng.replace(/^.*\.(.*)$/, '$1')     ($1 refers to
>whatever is in the first set of parenthesis.
>
>Change the position of the parenthesis to capture the filename minus
>the extention:
>
>/^(.*\.).*$/
>newName = strng.replace(/^(.*\.).*$/, '$1.working')  (The period
>needn't be escaped here.)
>
>Use a better regex, and match both:
>
>/^(.*\.)(.*)$/
>oldExt = = strng.replace(/^(.*\.)(.*)$/, '$2')
>newName = strng.replace(/^(.*\.)(.*)$/, '$1.working')
>
>Please let me know if you have any questions. I just typed all of this
>off of the top of my head, so it's possible there will be a syntax
>error or something. But I'll help you work through any problems (yours
>or mine).
>
>Shawn
>_______________________________________________
>Javascript mailing list
>Javascript at LaTech.edu
>https://lists.LaTech.edu/mailman/listinfo/javascript




More information about the Javascript mailing list