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.

1

5 Answers 5

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

Sign up to request clarification or add additional context in comments.

1 Comment

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.)
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

Comments

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

Comments

1

Try

var url = window.location.pathname;
value = url.replace('http://domain/catergory/','');
value = value.substring(0, s.indexOf('?'));

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.