1

I'm trying to replace the values for "min" and "max" price within the following URL string:

var url_full = "http://foo.com/?q=&min=0&max=789"

var url_clean = url_full.replace('&min='+ /\d+/,'');
var url_clean = url_full.replace('&max='+ /\d+/,'');

Struggling to replace the prices.

2
  • 1
    trying to replace values with what? empty? show inptu and output example Commented Oct 10, 2018 at 12:34
  • what is your expected output? Commented Oct 10, 2018 at 12:58

3 Answers 3

1

Replacing min and max values with ''

var url_full = "http://foo.com/?q=&min=0&max=789&hellomin=350"

var url_clean = url_full.replace(/&min=\d+/,'&min=').replace(/&max=\d+/,'&max=')
   
console.log(url_clean);

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

2 Comments

What if they have another parameter in that string called hellomin? Might be useful to keep searching for the &.
Right! I've fixed it
0
> var url = new URL(window.location.href);
> url.searchParams.set('min','100'); window.location.href = url.href;

Comments

0

var url_full = "http://foo.com/?q=&min=0&max=789"

var url_clean = url_full.replace('&min='+ /\d+/,'');
var url_clean = url_full.replace('&max='+ /\d+/,'');
console.log(url_clean);

var regex = /\d+/g;
var string = "http://foo.com/?q=&min=0&max=789";
var matches = string.match(regex);  // creates array from matches
for(var s=0;s<matches.length;s++){
console.log(matches[s]);
}
document.write(matches);

Use regex and match for finding digits.

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.