views:

49

answers:

3

Hi,

On my web app, I take a look at the current URL, and if the current URL is a form like this:

http://www.domain.com:11000/invite/abcde16989/root/index.html

-> All I need is to extract the ID which consists of 5 letters and 5 numbers (abcde16989) in another variable for further use.

So I need this:

var current_url = "the whole path, not just the hostname";

if (current_url has ID)
    var ID = abcde16989;
+4  A: 

You could always use split using / as the delimiter if the ID is always going to be in the same position, eg

var parts = current_url.split('/');
var id = parts[4];

Though your requirement of matching "5 letters and 5 numbers" really does suit a regex match.

var id = current_url.match(/[a-zA-Z]{5}[0-9]{5}/); // returns null if not found
Phil Brown
of course technically the split() parameter is still a regex. (what this proves of course is that simple regexes aren't actually as hard as the questioner thinks they are)
Spudley
thx. It can be different. http://www.domain.com:11000/root/index.html
Johua
@Spudley: Thanks for the insight();
Johua
I'm not sure how `split` works internally but I would have assumed it used a string tokenizer
Phil Brown
So, i nuderstand this would mean, find alfabet small and big letters {5} means five of them. Same goes for numbers, 0-9 five of them. If they are found id = result. THX. This regex is ok, but very often they get ver pretty. check
Johua
Yes. `A-Z` is a range, the square brackets mean "any single character in this set". The {5} is an occurrence qualifier meaning "this many times".
Phil Brown
+1  A: 

I'm assuming you don't need the full URL, but just the pathname to get your ID. Use the following:

var current_url = window.location.pathname; //gets the pathname
var split_url = current_url.split('/'); //splits the path at each /
current_id = split_url[2]; //1st item in array is "invite", 2nd is your id, 3rd would be "root"
alert(current_id);
moontear
+1  A: 

Firstly, this doesn't need JQuery; this is simple Javascript. I'll amend your tags after I've replied to reflect this.

A regex would actually be quite an easy way to achieve this, and I don't think a simple one like this would be as difficult to understand as you think.

So I'll answer with the regex option anyway and then move on to other options:

var url = "http://www.domain.com:11000/invite/abcde16989/root/index.html";

//first method:
var id = url.match('^http://www.domain.com:11000/invite/(.+)/root/index.html$')[1];/index.html$/)[1];

//second method: (if you don't know exact format of the rest of the URL but you do know the format of the ID string)
var id = url.match('/([a-z]{5}[0-9]{5})/')[1];

The first method will get the string in the position you specified within the URL. It won't check the formatting; it just looks at the rest of the URL and grabs the bit of it you're asking for. This should be really easy to understand: It's basically just your URL, but with (.+) where your ID goes.

The second method looks specifically for a string in the format you asked for -- ie five letters and then five numbers. This is admittedly a bit harder to read, but should be fairly self explanatory if you look at it given those criteria.

In both cases, the regex itself will return an array of results, with array element zero being the whole string (ie in the first case, including the rest of the URL). This is where the (brackets) come in (ie the bit where we said (.+)). This tells the match function to put the contents of the brackets into another array element so we can use it. In both cases, this means that we can read the ID in array element [1].

Okay, so how about the non-regex options:

In fact, it's going to be quite hard to do it in a simple way without regex in Javascript, since even the simple string splitting function uses a regex match to do the split (granted it would be a very simple one, it is still a regex). A couple of other people have already given you answers using this, but it is still a regex, so technically they've also not answered your question accurately.

I'm going to guess that actually one of these answers will be good enough for you (either mine or more likely one of the answers using split()), despite there still being a regex element. However if you really don't want anything to do with regex, you're going to have to start doing some slightly more complex string manipulation, probably using substring() (though there are other ways to do it).

Something along the lines of this:

var prefixstring="http://www.domain.com:11000/invite/";
var prefixlen=prefixstring.length;
var idlen=10;

var id = url.substring(prefixlen,idlen+prefixlen);

This gets the length of the portion of the URL in front of the ID, and then uses substring() to snip out the required bit. But I'm sure you'll agree that the regex options are simpler? ;-)

Hope that helps. (and I hope it helps you feel less afraid of regex!)

Spudley
One-up. I already solved my problem as soon as the first answer solved my problem with a very simple regex, i think i might need to rethink it when posting the question. Thanks to you all.
Johua