views:

140

answers:

6

Hi folks

I have a URL that looks like http://mysite.com/#id/Blah-blah-blah, it's used for Ajax-ey bits. I want to use substring() or substr() to get the id part. ID could be any combination of any length of letters and numbers.

So far I have got:

var hash = window.location.hash;
alert(hash.substring(1)); // remove #

Which removes the front hash, but I'm not a JS coder and I'm struggling a bit. How can I remove all of it except the id part? I don't want anything after and including the final slash either (/Blah-blah-blah).

Thanks!

Jack

+1  A: 

perhaps a regex

window.location.hash.match(/[^#\/]+/)
Paul Creasey
This regex will only match 1 character.
adamse
+1  A: 

Now, this is a case where regular expressions will make sense. Using substring here won't work because of the variable lengths of the strings.

This code will assume that the id part wont contain any slashes.

var hash = "#asdfasdfid/Blah-blah-blah";
hash.match(/#(.+?)\//)[1]; // asdfasdfid
  • The . will match any character and
  • together with the + one or more characters
  • the ? makes the match non-greedy so that it will stop at the first occurence of a / in the string

If the id part can contain additional slashes and the final slash is the separator this regex will do your bidding

var hash = "#asdf/a/sdfid/Blah-blah-blah";
hash.match(/#(.+?)\/[^\/]*$/)[1]; // asdf/a/sdfid

Just for fun here are versions not using regular expressions.

No slashes in id-part:

var hash = "#asdfasdfid/Blah-blah-blah",
    idpart = hash.substr(1, hash.indexOf("/"));

With slashes in id-part (last slash is separator):

var hash = "#asdf/a/sdfid/Blah-blah-blah",
    lastSlash = hash.split("").reverse().indexOf("/") - 1, // Finding the last slash
    idPart = hash.substring(1, lastSlash);
adamse
It should be `[1]`, since you want the captured group.
Matthew Flaschen
Just realized that :)
adamse
Excellent, full-featured answer, thanks adamse!
Jack Webb-Heller
A: 

Use IndexOf to determine the position of the / after id and then use string.substr(start,length) to get the id value.

var hash = window.location.hash;
var posSlash = hash.indexOf("/", 1);
var id = hash.substr(1, posSlash -1)

You need ton include some validation code to check for absence of /

Raj
@Erik, he's not *using* `substring`. The second parameter to [`substr`](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/substr) is the length, so this is correct.
Matthew Flaschen
+1  A: 
var hash = window.location.hash;
var matches = hash.match(/#(.+?)\//);
if (matches.length > 1) {
    alert(matches[1]); 
}
Darin Dimitrov
A: 

The most foolproof way to do it is probably the following:

function getId() {
    var m = document.location.href.match(/\/#([^\/&]+)/);
    return m && m[1];
}

This code does not assume anything about what comes after the id (if at all). The id it will catch is anything except for forward slashes and ampersands.

If you want it to catch only letters and numbers you can change it to the following:

function getId() {
    var m = document.location.href.match(/\/#([a-z0-9]+)/i);
    return m && m[1];
}
Roy Sharon
A: 

This one is not a good aproach, but you wish to use if you want...

var relUrl = "http://mysite.com/#id/Blah-blah-blah";
var urlParts = [];
urlParts = relUrl.split("/"); // array is 0 indexed, so 
var idpart = = urlParts[3] // your id will be in 4th element
id = idpart.substring(1) //we are skipping # and read the rest 
FallenAngel