wilks
wilks

Reputation: 13

Using replace on string with random number

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.

Upvotes: 1

Views: 70

Answers (3)

Sumesh TG
Sumesh TG

Reputation: 2575

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.

Upvotes: 0

kapantzak
kapantzak

Reputation: 11750

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);

Upvotes: 1

sathish
sathish

Reputation: 800

> var url = new URL(window.location.href);
> url.searchParams.set('min','100'); window.location.href = url.href;

Upvotes: 0

Related Questions