biillitil
biillitil

Reputation: 141

remove url parameters using javascript

I would like this url:

http://bulk-click.startappsdasdasice.com/tracking/adClick?d=scsdc%20cdcsc%20c

to become

bulk-click.startappsdasdasice.com/tracking

I need this kind of pattern for all urls. so the string with the question mark and onward need to be deleted

Upvotes: 3

Views: 317

Answers (4)

Good idea. But use lenght const, because it assigns meaning specifically to this URL, and not to all the meanings of the URL above

const myRe = new RegExp('([http|https]://)(.+)(/.+\?)', 'g'); const myArray = myRe.exec('http://bulk-click.startappsdasdasice.com/tracking/adClick?d=scsdc%20cdcsc%20c'); console.log(myArray[2]);

Upvotes: 0

Gerard
Gerard

Reputation: 15796

You may want to consider using a regular expression

var myRe = new RegExp('([http|https]://)(.+)(\/.+\?)', 'g');
var myArray = myRe.exec('http://bulk-click.startappsdasdasice.com/tracking/adClick?d=scsdc%20cdcsc%20c');
console.log(myArray[2]);

Upvotes: 1

zb22
zb22

Reputation: 3231

You can use the URL interface to achieve this and substring till the last pathname (/adClick)

let url = new URL('http://bulk-click.startappsdasdasice.com/tracking/adClick?d=scsdc%20cdcsc%20c');

console.log(url.host + url.pathname.substring(0, url.pathname.lastIndexOf('/')))

URL lastIndexOf() substring()

Upvotes: 0

Cagri Tacyildiz
Cagri Tacyildiz

Reputation: 17610

There are several ways for this one of them is

var url="http://bulk-click.startappsdasdasice.com/tracking/adClick?d=scsdc%20cdcsc%20c";
var suburl=url.substring(0,url.lastIndexOf("/")).replace(/(^\w+:|^)\/\//, '');
console.log(suburl);

Upvotes: 3

Related Questions