I need to get the first word after slash in a url in javascript, I assume using a regex would be ideal.
Here\'s an idea of what the URLs can possibly look like :
Exploding an url in javascript can be done using the official rfc2396 regex:
var url = "http://www.domain.com/path/to/something?query#fragment";
var exp = url.split(/^(([^:\/?#]+):)?(\/\/([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/);
This will gives you:
["", "http:", "http", "//www.domain.com", "www.domain.com", "/path/to/something", "?query", "query", "#fragment", "fragment", ""]
Where you can, in your case, easily retrieve you path with:
var firstPortion = exp[5].split("/")[1]