0

I have URL like :

http://domain/catergory/Education?max-post=5/

How can I get Education from that URL. Education is in between "/" and "?".

Thanks for your help.

Hai Tien
  • 2,929
  • 7
  • 36
  • 55
  • You can use regular expressions or the `split` method (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split) – Guilherme Sehn Nov 12 '13 at 13:18

5 Answers5

3

You can use a regexp for it:

var url = 'http://domain/catergory/Education?max-post=5/';

var val = url.match(/\/([^\/\?]*)\?/)[1];

To understand the regexp you can use this site: http://regex101.com/r/aQ3yF1#javascript

Tibos
  • 27,507
  • 4
  • 50
  • 64
  • 1
    I got the hint, hope that link is more helpful. (And yes, i do find writing regexp to be much easier than reading regexps wrote by others.) – Tibos Nov 12 '13 at 13:24
3

You can use split, it splits a String object into an array of strings by separating the string into substrings.

var url = "http://domain/catergory/Education?max-post=5/";
var arr = url.split("?")[0].split("/");
var edu = arr[arr.length - 1]
console.log(edu);

DEMO

Satpal
  • 132,252
  • 13
  • 159
  • 168
1
function getQuery(key) {
    var queryStr = location.search.match(new RegExp(key + "=(.*?)($|\&)", "i"));
    if (!queryStr)
        return

    return queryStr[1];
}

var id = getQuery('id');
var comment = getQuery('comment');

Source

Community
  • 1
  • 1
1

Try

var url = window.location.pathname;
value = url.replace('http://domain/catergory/','');
value = value.substring(0, s.indexOf('?'));
Ladislav M
  • 2,157
  • 4
  • 36
  • 52
1
var url = "http://domain/catergory/Education?max-post=5/";
var arr = url.split("?")[0].split("y/");
var edu = arr[1]
console.log(edu);
Anup
  • 3,283
  • 1
  • 28
  • 37