0

I need to extract parameter name from the url path. parameters are inside {} or follow the first ?(questionmark) or follow & mark in the url.

the url path need to be extracted is in such format :

/tt/{pa}/{pb}/?p1=9888&p2=9&p3=98

thus the parameters are pa, pb,p1,p1,p3

witrus
  • 297
  • 1
  • 3
  • 13
  • I don't think you can get all your parameters with a single match – Maurice Perry Apr 17 '15 at 13:02
  • possible duplicate of [Getting all URL parameters using regex](http://stackoverflow.com/questions/14679113/getting-all-url-parameters-using-regex) – oliveromahony Apr 17 '15 at 13:11
  • @oliveromahony OP wants more than just the URL parameters, but definitely that could be part of the solution. – asontu Apr 17 '15 at 13:13
  • oh ya, sorry - didn't read it fully! – oliveromahony Apr 17 '15 at 13:20
  • I considered suggested RFC6570-style URI parsers, but you can't mix variables with and without concrete values (i.e. they can parse the path parameters in the example but they can't parse the query string variables as-is) – brabster Apr 17 '15 at 13:46

2 Answers2

2

Quickest way I can think of is this:

[{?&][^}=]+

Regular expression visualization

Debuggex Demo

Now I'm also capturing the { or ? or & character preceding it, so you'd have to cut that off afterwards:

var inp = '/tt/{pa}/{pb}/?p1=9888&p2=9&p3=98'

var found = inp.match(/[{?&][^}=]+/g).map(function(p){ return p.substring(1); });

document.write(found.join('<br />'));

And if you expect something like /tt/{pa=}/{pb}/?p1={9888&p2=9&p3=98 where pa= is a valid param name and/or {9888 is a valid value, you'd be in trouble. But I doubt this is a serious limitation.

asontu
  • 4,548
  • 1
  • 21
  • 29
0

Simple example for extracting separately search variables and variables from path:

function extractSearch(search) {
    var result = {};

    search = search || window.location.search;

    if (search.length) {
        search = search.substr(1).split('&');

        for (var i = 0; i < search.length; i++) {
            search[i] = search[i].split('=');
            result[decodeURIComponent(search[i][0])] = decodeURIComponent(search[i][1]);
        }
    }

    return result;
}

function extractFromPath(pattern, url) {
    var keys = [],
        result = {},
        i;

    url = url || window.location.pathname;

    pattern = pattern.replace(/{([^}]+)}/g, function($0, $1) {
        keys.push($1);
        return '([^/]+)';
    });

    pattern = url.match(new RegExp('^' + pattern));

    for (i = 0; i < keys.length; i++) {
        result[keys[i]] = pattern[i + 1];
    }

    return result;
}

Example of use:

// Object {a: "10", b: "20"}
extractSearch('?a=10&b=20');

// Object {pa: "1", pb: "2"}
extractFromPath('/tt/{pa}/{pb}', '/tt/1/2/?p1=9888&p2=9&p3=98');

Eventually you can combine it by:

function getParameters(pattern, path) {
    var result,
        tmp;

    path = path.split('?');

    result = extractFromPath(pattern, path[0]);

    if (path[1] !== void 0) {
        tmp = extractSearch('?' + path[1]);

        for (var param in tmp) {
            result[param] = tmp[param];
        }
    }

    return result;
}

// Object {pa: "1", pb: "2", a: "10", b: "20"}
getParameters('/tt/{pa}/{pb}', '/tt/1/2?a=10&b=20');
Dawid Rusnak
  • 527
  • 3
  • 13