deeej
deeej

Reputation: 387

How do I remove part of a string from a specific character?

I have the following url:

http://intranet-something/IT/Pages/help.aspx?kb=1

I want to remove the ?kb=1 and assign http://intranet-something/IT/Pages/help.aspx to a new variable.

So far I've tried the following:

var link = "http://intranet-something/IT/Pages/help.aspx?kb=1"

if(link.includes('?kb=')){
  var splitLink = link.split('?');
}

However this just removes the question mark.

The 1 at the end of the url can change.

How do I remove everything from and including the question mark?

Upvotes: 0

Views: 72

Answers (4)

Vijay Dwivedi
Vijay Dwivedi

Reputation: 172

You can also try like this

const link = "http://intranet-something/IT/Pages/help.aspx?kb=1";
const NewLink = link.split('?');
console.log(NewLink[0]);

Upvotes: 0

Divyanshi Mishra
Divyanshi Mishra

Reputation: 110

var link = "http://intranet-something/IT/Pages/help.aspx?kb=1"
if(link.includes('?kb=')){
  var splitLink = link.split('?');
  console.log(splitLink[0]);
}

Upvotes: 0

C.V
C.V

Reputation: 969

var link = "http://intranet-something/IT/Pages/help.aspx?kb=1"

if (link.includes('?kb=')) {
  var splitLink = link.split('?');
}

var url = splitLink ? splitLink[0] : link;

console.log(url);

Upvotes: 2

deceze
deceze

Reputation: 521995

Use the URL interface to manipulate URLs:

const link = "http://intranet-something/IT/Pages/help.aspx?kb=1";
const url = new URL(link);
url.search = '';
console.log(url.toString());

Upvotes: 4

Related Questions