1
var url = 'http://mywebsite.com/?&filters[]=price_gte:20';
var path = '&filters[]=price_gte:';
var value = 10;

How do I replace the &filters[]=price_gte:20 part with the new path? (path + value)

I've tried RegExp():

var re = new RegExp(path);
console.log(url.replace(re, ''));
//returns http://mywebsite.com/?&filters[]=price_gte:20

That didn't even work, let alone adding the digit in the regex. Problem is, price_gte is a variable (and so will price_lte be, and whatever other ranges there might come), with a variable digit after it.

Information I have:

  • url (http://mywebsite.com?&filters[]=price_gte:20&filters[]=price_lte:100)
  • path (&filters[]=price_gte:)
  • new value (10)

Desired result:

  • http://mywebsite.com?&filters[]=price_gte:10&filters[]=price_lte:100

What am I missing?

Mave
  • 2,413
  • 3
  • 28
  • 54
  • You missed the `\d{1,}` – mplungjan May 13 '15 at 13:41
  • 2
    brackets `[]` is special symbol in regex, so it should be escaped. – Grundy May 13 '15 at 13:45
  • You are using the wrong tool for the job or at least not in the right way. Stop thinking about string replacement and see it as URI parsing and reconstruction. Parsing the query string and yield a data structure out of it (e.g. key/value pairs) that can be manipulated and re-serialized as a query string shouldn't be hard and will be much more solid and flexible. – plalx May 13 '15 at 13:48
  • 1
    @plalx parsing query string can be more complicated than simple replace – Grundy May 13 '15 at 13:54
  • In this case it works just fine to do parsing. See my second example – mplungjan May 13 '15 at 14:01
  • @Grundy True, but as soon as complexity will arise in filters management you will feel the pain of a fragile design and to be fair, none of the proposed regex solutions deals with edge cases (multiple filters of the same name, filter not preceded by &, other keys contains `filters` in their name, params encoding, etc... Obviously, I'm not saying solutions cannot be adjusted to be more robust, but then a simple parser would have been more efficient and maintainable. – plalx May 13 '15 at 14:41
  • @plalx Other filters are fine, even same names. `&filters[]=Colour_en:Blue&filters[]=Colour_en:Red` replace fine, simply by checking if current values (Colour_en and Blue) match current string. If true, replace it with nothing (so `?&filters[]=Colour_en:Blue` becomes `?`). If false, `?` becomes `?&filters[]=Colour_en:Blue`. Combine it however you want. – Mave May 13 '15 at 14:47
  • @Mave Wouldn't you prefer doing something like `var filters = Filters.fromQueryString(qs, 'filters[]'); filters.set('price_gte', 10); var filteredQs = filters.injectIntoQueryString(qs, 'filters[]');` where `qs` is a query string. Making implicit concepts explicit helps in yielding more maintainable and testable designs. – plalx May 13 '15 at 15:01

5 Answers5

1

Perhaps you meant this

var url = 'http://mywebsite.com/?&filters[]=price_gte:20';
var path = 'filters[]=price_gte:';
var value = 10;
var re = /filters\[\]=price_[g|l]te:\d+/;
alert(url.replace(re,path+value))

However a better solution might be

// from http://stackoverflow.com/a/20420424/295783

function replaceUrlParam(url, paramName, paramValue){
    var pattern = new RegExp('('+paramName+'=).*?(&|$)')
    var newUrl=url
    if(url.search(pattern)>=0){
        newUrl = url.replace(pattern,'$1' + paramValue + '$2');
    }
    else{
        newUrl = newUrl + (newUrl.indexOf('?')>0 ? '&' : '?') + paramName + '=' + paramValue 
    }
    return newUrl
}

var url = 'http://mywebsite.com/?&filters[]=price_gte:20';
var value=10;

url = replaceUrlParam(url,"filters[]", 'price_gte:'+value);
alert(url)
mplungjan
  • 169,008
  • 28
  • 173
  • 236
1

You were almost there, just needed to escape the brackets:

var url = 'http://mywebsite.com/?&filters[]=price_gte:20';
var path = '&filters[]=price_gte:';
var regexPath = '&filters\\[\\]=price_gte:\\d+';
var value = 10;
var re = new RegExp(regexPath);

console.log(url.replace(re, path+value));
Rodrigo López
  • 4,039
  • 1
  • 19
  • 26
0

This seems to work for me:

function replace() {
                var url = 'http://mywebsite.com?&filters[]=price_gte:20&filters[]=price_lte:200';
                var path = '&filters[]=price_gte:';
                var value = 10;

                var newStr = url.replace(/&filters\[\]=price_gte:\d+/, "&filters[]=price_gte:" + value);
                alert(newStr);
            }
npinti
  • 51,780
  • 5
  • 72
  • 96
0

This is enough for getting new value

var str = 'http://mywebsite.com?&filters[]=price_gte:20&filters[]=price_lte:100';
var pattern = /price_gte:\d+/;
var new_value = 10;
str = str.replace(patter, 'price_gte:'+new_value);
Samir Das
  • 1,878
  • 12
  • 20
0

And Simple Solution will be =>

function replace() {
            var url = 'http://mywebsite.com?&filters[]=price_gte:20&filters[]=price_lte:200';
            var path = '&filters[]=price_gte:';
            var value = 10;

            var newStr = url.replace(/(\w*)(price_gte:)(\d*)/i,"price_gte:"+ value);
            alert(newStr);
        }
pg200890
  • 11
  • 2