Closed Thread Icon

Preserved Topic: Simple RegExp Pages that link to <a href="https://ozoneasylum.com/backlink?for=18280" title="Pages that link to Preserved Topic: Simple RegExp" rel="nofollow" >Preserved Topic: Simple RegExp\

 
Author Thread
Smash
Bipolar (III) Inmate

From: Stockholm, Sweden
Insane since: Sep 2001

posted posted 09-11-2001 13:40

This question goes to anyone on the Asulym, but i KNOW the guru mr.MaX will solve it for me :-]

I have random URL´s that I want to remove the last file from...

ex. http://www.someserver.com/somefolder/file.html
would be: http://www.someserver.com/somefolder

How do I do that with a simple regExp?

Thanks in advance

Smash!

mr.maX
Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 09-11-2001 14:20

<SCRIPT TYPE="text/javascript" LANGUAGE="JavaScript">
<!-- ;

// Written by mr.maX, http://www.max.co.yu/

maxURL = "http://www.someserver.com/somefolder/file.html";
maxURL = maxURL.replace(/[^\/]+$/ig, "");
document.write(maxURL);

// -->
</SCRIPT>

Enjoy!

Dracusis
Maniac (V) Inmate

From: Brisbane, Australia
Insane since: Apr 2001

posted posted 09-11-2001 14:26

Dammit MAX! I could have actualy answered that one! heh

lallous
Paranoid (IV) Inmate

From: Lebanon
Insane since: May 2001

posted posted 09-11-2001 14:59

My turn now

Yet a simpler one:

code:
url = "http://www.someserver.com/somefolder/file.html".match(/(.+?)[^/]+$/i);
document.write(RegExp.$1);



lallous
Paranoid (IV) Inmate

From: Lebanon
Insane since: May 2001

posted posted 09-11-2001 15:08

Now mr.Max your turn again!

Me and crossbrowsers! this won't work on ShiftScape!

why?!

other than escaping the / in [^/] like [^\/]...

I want the same RegExp but want to know why ShiftScape doesn't work!

mr.maX
Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 09-11-2001 23:56

Lallous, actually your code isn't simpler than mine. Only thing that you did is condense functions, but your RegEx is still more complex than mine. Anyway, I won't go into cross browser issues since I don't have time.

BTW This is my code in condensed form:

document.write("http://www.someserver.com/somefolder/file.html".replace(/[^\/]+$/ig, ""));

Short, isn't it?

lallous
Paranoid (IV) Inmate

From: Lebanon
Insane since: May 2001

posted posted 09-12-2001 09:00

I now beleive that NetScape doesnt fully support PCRE (Perl Compatible Regular Expression) as opposite to JavaScript, PHP, Delphi's TRegExpr unit, and many other RegExps librarys that i'm testing on!

I'm not gonna dive too into solving the matter of ShiftScape (as you call it)....

As for your approach for Smash's question..well it's not a good use of RegExps as you did!
...it could be solved the same way without RegExps:

code:
str = "http://www.someserver.com/somefolder/file.html";
document.write(str.substr(0, str.lastIndexOf("/")+1));



mr.maX
Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 09-12-2001 17:58

Of course that it can be done using only string handling functions, but he asked for RegEx and I wrote it...

lallous
Paranoid (IV) Inmate

From: Lebanon
Insane since: May 2001

posted posted 09-12-2001 20:54

of course mr.Max but what I really mean that using the Replace method is not the optimal way to extract information...it should have been a pure Regular expression that extracts the info...

mr.maX
Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 09-12-2001 21:30

IMHO Matching part of the string that should be excluded is more efficient in this case (especially since filename will be shorter than the rest of the URL)...

« BackwardsOnwards »

Show Forum Drop Down Menu