繁体   English   中英

获取此URL的最后一部分

[英]get last part of this URL

我从Twitter按钮的Object中抓取了这个奇怪的URL:

http://platform.twitter.com/widgets/tweet_button.1329950604.html#_=1330013339435&count=none&hashtags=allstar&id=twitter-widget-10&lang=en&original_referer=http%3A%2F%2Fwww.nba.com%2Fpulse%2Fallstar% 2F2012%2Findex.html&相关=%40nba&大小= M&文本=精灵%20Slam%20Dunk%20Contest%图20是%20trending%20on%20NBA%20All明星%20Pulse%20&URL = HTTP%3A%2F%2Fwww.nba.com%2Fpulse%2Fallstar %2F2012%2Ftopics%2Fspriteslamdunk.html

我需要在'.html'之前得到最后一部分,在上面的例子中是:spriteslamdunk

这似乎出现在%2Ftopics%2F之后

不知道如何用jQuery提取它?

[编辑]看起来我必须用正则表达式做? 在这方面没有对Regex做过多少工作?

我想你可能不得不使用正则表达式"(\\w+)\\.html$"

var answer = url.substring(url.lastIndexOf(“%2F”)+ 3).replace(“。html”,“”)

您想在.html之前和%2F的最后一次出现之后检索字符串。

你可以使用正则表达式/%2F([^%]+?)(\\.html$)/ \\.html$与上次出现之前的字符串匹配; ([^%]+?)部分匹配最短的字符串,该字符串不包含%和之前的.html ,这是您要检索的内容; %2F在匹配项中排除此字符串。

var str = "http://platform.twitter.com/widgets/tweet_button.1329950604.html#_=1330013339435&count=none&hashtags=allstar&id=twitter-widget-10&lang=en&original_referer=http%3A%2F%2Fwww.nba.com%2Fpulse%2Fallstar%2F2012%2Findex.html&related=%40nba&size=m&text=Sprite%20Slam%20Dunk%20Contest%20is%20trending%20on%20NBA%20All-Star%20Pulse%20&url=http%3A%2F%2Fwww.nba.com%2Fpulse%2Fallstar%2F2012%2Ftopics%2Fspriteslamdunk.html"

var matches = str.match(/%2F([^%]+?)(\.html$)/)

matches
// => ["%2Fspriteslamdunk.html", "spriteslamdunk", ".html"]

matches[1] // What you want.

暂无
暂无

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

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