3gwebtrain
3gwebtrain

Reputation: 15303

get specific part from url

my window.location is "F:/html5/home.html", from my location i need to get the file name like this "home.html", to do this, how to i use the regular expression command?

any one help me?

Upvotes: 0

Views: 670

Answers (2)

Amarghosh
Amarghosh

Reputation: 59471

This will do it

[^/]+$

It matches the substring of non-forward-slash characters towards the end of the string.

var s = "F:/html5/home.html";
//forwards slashes must be escaped in JavaScript regex as it is the delimiter
alert(s.match(/[^\/]+$/)[0]);

Upvotes: 2

Khan
Khan

Reputation: 2992

If you're flexible about not using regular expressions, I would do this:

var pathArr = new Array();
pathArr = window.location.split("/");
var file = pathArr[pathArr.length - 1];

Upvotes: 2

Related Questions