0

I need to change the value of query parameters of the given URL irrespective of value type.

I have a function like,

function urlGenerator (key, value) {
    var url = "www.domain.com/?id=10&name=xxx&dob=2018-12-13";
    // change the value for given key
    return url;
}

The order of query parameters are not fixed. It may be of any order. I think the solution can be found by Regex.

I need generic solution irrespective of the type of value given to above specified URL. I need to call the above method like following and must get the resultant URL.

result = urlGenerator('id', 15);
result = urlGenerator('name', 'yyy');
result = urlGenerator('dob', '2018-10-20');

Thanks in advance!!

6
  • You would need to parse the url to get an object with all parameters, add / change the value you need to change and rebuild the url. See for example stackoverflow.com/questions/8486099/… for the first step. Commented Dec 13, 2018 at 9:00
  • why is there a php tag here? Commented Dec 13, 2018 at 9:07
  • @mickmackusa While adding regex tag, SO suggested to add the language. Commented Dec 13, 2018 at 9:10
  • javascript is a language ...and happens to be the one that you are using. Commented Dec 13, 2018 at 9:11
  • The url is static in your function? Commented Dec 13, 2018 at 9:11

2 Answers 2

1

You can do it like this

So here in temp variable we are building a expression for regex using string template and than using RegExp change it to regex object.And then by using replace method we replace the matched value with the value supplied in function.

function urlGenerator (key, value) {
    let temp = '(?<='+`${key}`+'=)[^&]+'
    let reg = new RegExp(temp,'g');
    console.log(reg);
    var url = "www.domain.com/?id=10&name=xxx&dob=2018-12-13";
    // change the value for given key
    return url.replace(reg, value);
}

result = urlGenerator('id', 15);
console.log(result);
result = urlGenerator('name', 'yyy');
console.log(result);
result = urlGenerator('dob', '2018-10-20');
console.log(result);

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

2 Comments

@VinothKumar always happy to help :)
@Jouby I have tried your 1st solution but that doesn't work as expected. Before your edited answer, this answer is posted and it works fine. That's why I haven't replied to your answer.
1

I use replace to change value by regex.

Try this urlGenerator function :

function urlGenerator(url, field, value) {
    var reg = new RegExp('('+field+'=[^&]*)', "");
    return url.replace(reg, field+'='+value);
}

EDIT:

If you wan't a static url in your function :

function urlGenerator(field, value) {
    var url = "www.domain.com/?id=10&name=xxx&dob=2018-12-13";
    var reg = new RegExp('('+field+'=[^&]*)', "");
    return url.replace(reg, field+'='+value);
}

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.