jQuery删除""之后的字符串的一部分#"并且去除"#""

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

本文关键字:quot 删除 jQuery 字符串 一部分 之后      更新时间:2023-09-26

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

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

简单如

您可以直接使用split。

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

请参阅:http://jsfiddle.net/DGFuf/1/

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

试试这个:

var str = "example.com/hello#item1";
    var newStr = str.substring(0, (str.length - str.indexOf("#")));

您也可以使用这个:

var str = "example.com/hello#item1".split('#')[0];
location.href.replace(location.hash,"")

那就行了

编辑

哎呀。。。一串嗯……好吧,这就是从url XDDD 中删除它的方法