Skip to main content
Known Participant
September 30, 2005
Question

Buglet in AdobeLibrary1.jsx

  • September 30, 2005
  • 9 replies
  • 1117 views
I thought I'd point out a technical bug in one of the functions in AdobeLibrary1.jsx. The function file.getExtension() can return wrong results when the filename has no extension because (on Windows, at least) parts of the pathname can have "." in them. This isn't real common, but is possible. So if you pass the filename "c:/bob/ted.alice/jack" to your function, it will return ".alice/jack" as the extension which is not technically correct. In reality, it should return "" since there is no extension.

In my own code, I've used a regular expression /\.[^\/\\]*$/ which is a period, followed by some number of non-path separator characters at the end of the string and this handles both cases.

--John
This topic has been closed for replies.

9 replies

Known Participant
October 1, 2005
Thanks Andrew and xbytor. It's good to know how that works.

--John
Inspiring
October 1, 2005
I want to thank you guys for this discussion. You inspired me to write a script for testing Regex expressions in conjunction with InDesign -- select some text in InDesign and then run the script. It invites you to type (or paste) a regex string into a prompt and then gives you the result and any matched substrings.

I've posted a description here: Dave Saunders, "JS: Regex Driving Me Nuts" #22, 1 Oct 2005 6:41 am with a better version in the next message.

So thanks for the above.

Dave
Known Participant
October 1, 2005
I tested that exec method and it seems to work - I've been wanting something like that for a long time:

var test = 'cdaabcbde';
re = /a(b.*$)/;
matchAr = re.exec(test);
alert(matchAr[1]);// returns bcbde

Andrew
Known Participant
October 1, 2005
I think you need to use brackets with the exec method of the re eg

var re = /\.([^\.\/\\]+$)/;
var reResultsAr = re.exec(targetstring);
var firstBracket = reResultsAr[1];

I better admit that I have not tested this, I'm just going from the Core Javascript Ref 1.5.

Andrew
Known Participant
October 1, 2005
Andrew-Hall@adobeforums.com wrote:
> I think you need to use brackets with the exec method of the re eg
>
> var re = /\.([^\.\/\\]+$)/;
> var reResultsAr = re.exec(targetstring);
> var firstBracket = reResultsAr[1];
>
> I better admit that I have not tested this, I'm just going from the Core Javascript Ref 1.5.
>
> Andrew

I knew about doing this, but he wanted a one liner, i.e. one that just used
regexp to return a result. It's not possible with JavaScripts regexp implementation.

Also, you don't need to use RegExp.exec: String.match will give you the same
behaviour.

And, if you really want to get it all on one line, do it like this:

((RegExp.__tmp = str.match(/\.([^\.\/\\]+$)/)) ? RegExp.__tmp[1] : undefined)

While something like this is (almost) obvious to me, I can't say that most PS
scripters would understand it at first glance.

ciao,
-X
Known Participant
September 30, 2005
> You may also want to try this variant: /\.[^\.\/\\]+$/

xbytor or anyone else, do you know how to construct a regular expression that will match the same thing as above, but not include the leading period in the matched result? I know there are reg ex ways to do something like this, but I've not been able to get it to work when I've tried. In my code that follows this, I remove the leading period in code, but I'd like to know how to remove it using the regular expression, if possible.

--john
Known Participant
September 30, 2005
jfriend00@adobeforums.com wrote:
>
> You may also want to try this variant: /\.[^\.\/\\]+$/
>
> xbytor or anyone else, do you know how to construct a regular expression that will match the same thing as above, but not include the leading period in the matched result? I know there are reg ex ways to do something like this, but I've not been able to get it to work when I've tried. In my code that follows this, I remove the leading period in code, but I'd like to know how to remove it using the regular expression, if possible.
>
> --john

In perl, it would look something like:
/(?<=\.)[^\.\/\\]+$/

But we don't have ?<= in JavaScript. Off the top of my head, I can't think of
another way to do it.

ciao,
-X
Known Participant
September 30, 2005
> Another construction that this doesn't work on is "c:bob.ted" or "bob.ted", though neither of these are fully qualified paths and that's all I'm using it on.

Never mind on that comment. This reg ex works fine on those constructions. I was thinking of a different function of mine which returns the base filename (without path) that has a problem with those constructions.

--John
Known Participant
September 30, 2005
Ahh, you're right. You can have multiple periods in the last segment. That's a good improvement to add the . with the brackets and make sure we get only the segment after the last period.

I conciously thought about using + instead of *. I can't remember at the moment why I thought there was a problem with that.

Another construction that this doesn't work on is "c:bob.ted" or "bob.ted", though neither of these are fully qualified paths and that's all I'm using it on.

--John
Known Participant
September 30, 2005
jfriend00@adobeforums.com wrote:
> In my own code, I've used a regular expression /\.[^\/\\]*$/ which is a period, followed by some number of non-path separator characters at the end of the string and this handles both cases.

Good catch.

You may also want to try this variant: /\.[^\.\/\\]+$/

This will do the right thing for strings like "ssss." and "ss.ss.xx". Adding the
'\.' makes sure you get the last '.' and the '+' guarantees there is at least
one appropriate character after that last '.'.

ciao,
-X
>
> --John
Known Participant
September 30, 2005
Oops

Thanks

Bob