views:

56

answers:

3

Hi There

I am hoping that someone can help me, I am using Javascript and asp

I have this string /folder1/folder2/pagename.asp

I need a regex to strip the /pagename.asp from the string

pagetype.replace(/\/.*?\.asp/ig, '');

The regex above is halfway there but the problem is that it strippes from the beginning / everything between the / and the .asp

how do I make the regex lazy so that it will only strip between the last / and the .asp?

Any help would be appreciated!

+2  A: 
/(.*)\/.*?\.asp/ig

The problem is that the regex is matching as early as possible - one solution is to put a greedy quantifier at the start so that it gobbles up all the possible early matches and you get the last one.

And we use a capturing group to grab that bit so we can keep it around.

Anon.
Thanks Anon - works 100%
Gerald Ferreira
+2  A: 

Try this:

pagetype.replace(/\/[^\/]*\.asp/ig, '');

I've used [^\/] group to represent any symbol not equal to /.

Ivan Nevostruev
Thanks Ivan - works 100%
Gerald Ferreira
+1  A: 

Sure use this, will strip off the page name and extension:


(\w+)(\.\w+)(:[0-9]+)?(/.+)?
drlouie - louierd