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,556
  • 7
  • 32
  • 49
  • 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
  • 26,984
  • 4
  • 46
  • 61
  • 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
  • 129,808
  • 12
  • 152
  • 166
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,107
  • 4
  • 33
  • 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,205
  • 1
  • 26
  • 37