Paul
Paul

Reputation: 13

What Regular Expression can I use to remove all characters from a url after the last /

When given a full URL I need a regular expression that I can use to replace everything after the final slash with nothing, i.e.

http:// www.somewhere.com/someplace/subsomeplace/default.aspx

would return

http:// www.somewhere.com/someplace/subsomeplace/

and

http:// www.somewhere.com/someplace/subsomeplace

would return

http:// www.somewhere.com/someplace/

The spaces in the URLs above are not really there I needed to remove them because Stack Overflow won't let me post more then 2 URLs

Upvotes: 1

Views: 374

Answers (1)

Tomas
Tomas

Reputation: 714

/[^\/]*$/

This will give you the part you need.

Rubular link: http://rubular.com/r/zJlu1O5SqC

JavaScript example, tested in FireBug:

"http://www.somewhere.com/someplace/subsomeplace/default.aspx".match(/[^\/]*$/g);

Upvotes: 3

Related Questions