Get data from URL with Javascript RegExp
I have a consistent URL along the lines of: h开发者_StackOverflow社区ttp://www.site.com/user/Ryan
and I want to retreive the domain extension and the username, as to say: http://www.site.(*)/user/(*)
I have two options (afaik) split() or a regexp, split() doesn't really sound too stable (also since the extension could be 'com' or 'com.br', so I'll probably like to use the regexp option, but I have no idea how get started on this one..
var re = /http:\/\/www.site.([^\/]*)\/user\/(\w*)/
var tokens = 'http://www.site.com.br/user/Ryan'.match(re);
var theWholeUrl = tokens[0];
var domain = tokens[1];
var username = tokens[2];
alert('theWholeUrl: ' + theWholeUrl);
alert('domain: ' + domain);
alert('username: ' + username);
精彩评论