2

I have a requirement of removing a query parameter coming with a REST API call. Below are the sample URLs which need to be considered. In each of this URL, we need to remove 'key' parameter and its value.

  • /test/v1?key=keyval&param1=value1&param2=value2
  • /test/v1?key=keyval
  • /test/v1?param1=value1&key=keyval
  • /test/v1?param1=value1&key=keyval&param2=value2

After removing the key parameter, the final URLs should be as follows.

  • /test/v1?param1=value1&param2=value2
  • /test/v1?
  • /test/v1?param1=value1
  • /test/v1?param1=value1=&param2=value2

We used below regex expression to match and replace this query string in php. (https://regex101.com/r/pK0dX3/1)

(?<=[?&;])key=.*?($|[&;])

We couldn't use the same regex in java script. Once we use it in java script it gives some syntax errors. Can you please help us to figure out the issue with the same regex ? How can we change this regex to match and remove query parameter as mentioned above?

Mena
  • 47,782
  • 11
  • 87
  • 106
thilinistg
  • 407
  • 3
  • 8
  • 18
  • "it gives some syntax errors" informations you are getting as error are probably important so consider posting them in your question. – Pshemo Jun 02 '15 at 14:38
  • 1
    Does [How can I delete a query string parameter in JavaScript?](http://stackoverflow.com/a/1634841/3832970) solve your problem? – Wiktor Stribiżew Jun 02 '15 at 14:41

4 Answers4

3

The regex works in PHP but not in Javascript because Javascript does not support lookbehind.

The easiest fix here would be to replace the lookbehind (?<=[?&;]) with the equivalent characters in a capturing group ([?&;]) and use a backreference ($1) to insert this bit back into the replacement string.

For example:

var path = '/test/v1?key=keyval&param1=value1&param2=value2';

var regex = /([?&;])key=.*?($|[&;])/;

console.log(path.replace(regex, '$1'); // outputs '/test/v1?param1=value1&param2=value2'

Not convinced regex would be the most reliable way of removing a query parameter, but that's a different story :-)

Shai
  • 7,159
  • 3
  • 19
  • 22
3

Obviously lookbehind isn't supported in Javascript hence your regex won't work.

In Javascript you can use this:

repl = input.replace(/(\?)key=[^&]*(?:&|$)|&key=[^&]*/gmi, '$1');

RegEx Demo

Regex is working on 2 paths using regex alternation:

  1. If this query parameter is right after ? then we grab till & after parameter and place ? back in replacement.
  2. If this query parameter is after & then &key=value is replaced by an empty string.
anubhava
  • 761,203
  • 64
  • 569
  • 643
1

The lookbehind feature isn't available in javascript, so to test the character before the key/value, you must match it. To make the pattern works whatever the position in the query part of the url, you can use an alternation in a non-capturing group, and you capture the question mark:

url = url.replace(/(?:&|(\?))key=[^&#]*(?:(?!\1).)?/, '$1');

Note: the # is excluded from the character class to prevent the fragment part (if any) of the url to be matched with key value.

Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125
1

Just in case you want to do it without a regex, here is a function that will do the trick:

var removeQueryString = function (str) {
    var qm = str.lastIndexOf('?');
    var path = str.substr(0, qm + 1);
    var querystr = str.substr(qm + 1);

    var params = querystr.split('&');
    var keyIndex = -1;
    for (var i = 0; i < params.length; i++) {
        if (params[i].indexOf("key=") === 0) {
            keyIndex = i;
            break;
        }
    }
    if (keyIndex != -1) {
        params.splice(keyIndex, 1);
    }

    var result = path + params.join('&');
    return result;
};
Venemo
  • 18,515
  • 13
  • 84
  • 125