简体   繁体   中英

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

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM