简体   繁体   English

如何从特定字符中删除部分字符串?

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

I have the following url:我有以下 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.我想删除?kb=1并将http://intranet-something/IT/Pages/help.aspx分配给一个新变量。

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. url末尾的1可以改变。

How do I remove everything from and including the question mark?如何从问号中删除所有内容,包括问号?

Use the URL interface to manipulate URLs:使用URL接口来操作 URL:

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

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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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