3

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

1
  • Doing a POST instead of a GET could solve your issue too. Commented Apr 27, 2020 at 19:07

4 Answers 4

3

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

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

2 Comments

Can u write one more example which is not working with my code in your question?
it works but i updated the question. i need to get rid of the http:// or https:// part as well
1

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

Comments

0

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

Comments

0

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

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.