Javascript jQuery删除“#”之后的部分字符串并删除“#”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7000995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 00:13:34  来源:igfitidea点击:

jQuery removing part of string after "#" and removing "# "too

javascriptstring

提问by krzyhub

I wonder how can i remove all string part after "#". For example if i have example.com/hello#item1how can i remove all characteres after #and #character too?

我想知道如何删除“#”之后的所有字符串部分。例如,如果我有example.com/hello#item1如何删除所有字符##字符之后?

回答by Senad Me?kin

var string = "example.com/hello#item1".split('#')[0];

simple as that

就那么简单

回答by Billy Moon

var str = "example.com/hello#item1";
str = str.replace(/#.*$/,'');

回答by ShankarSangoli

Try this:

尝试这个:

var str = "example.com/hello#item1";

    var newStr = str.substring(0, (str.length - str.indexOf("#")));

回答by Mrchief

You can also use this:

你也可以使用这个:

var str = "example.com/hello#item1".split('#')[0];

回答by CaffGeek

You can just use split.

您可以只使用拆分。

var oldString ='somestring#removethis';
var newString = oldString.split('#', 1)[0];
alert(newString);

see: http://jsfiddle.net/DGFuf/1/

见:http: //jsfiddle.net/DGFuf/1/

回答by Joseph Marikle

location.href.replace(location.hash,"")

that'll do it

就可以了

edit

编辑

oops... string... hm... well this is how you remove it from the url XDDD

哎呀...字符串...嗯...嗯,这就是你如何从 url XDDD 中删除它