jQuery URL 拆分和抓取

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9491721/
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-27 10:53:18  来源:igfitidea点击:

jQuery URL split and grab

jqueryurlsplit

提问by RussellHarrower

So I have a URL and I know how to get $_GET from URL however my URL is http://www.example.com/#!/edit/2695.

所以我有一个 URL,我知道如何从 URL 获取 $_GET 但是我的 URL 是http://www.example.com/#!/edit/2695.

Is there away to grab the url and spit the parts after #!/? I want edit and the ID.

有没有办法抓住网址并在之后吐出零件#!/?我想要编辑和 ID。

回答by jfriend00

You can use this code

您可以使用此代码

var url = "http://www.mysite.com/#!/edit/2695";
var pieces = url.split("/#!/");
pieces = pieces[1].split("/");

// pieces[0] == "edit"
// pieces[1] == "2695"

If you just wanted the number after the edit, you could also use a regex

如果你只想要编辑后的数字,你也可以使用正则表达式

var url = "http://www.mysite.com/#!/edit/2695";
var match = url.match(/#!\/edit\/(\d+)/);
if (match) {
    // match[1] == "2695"
}

You can see both of them work here: http://jsfiddle.net/jfriend00/4BTyH/

你可以在这里看到他们都在工作:http: //jsfiddle.net/jfriend00/4BTyH/

回答by adeneo

var edit = window.location.hash.split('/')[1],
      ID = window.location.hash.split('/')[2];