我在Twitter的按鈕抓住這個陌生的網址了一個對象:此網址的最後一部分
我需要的「 html的」,這在例如前拿到最後一部分上面是:spriteslamdunk
這似乎%2Ftopics%2F後出現
不知道如何提取使用jQuery?
[編輯]看起來我必須用正則表達式來做?在這方面與正則表達式沒有太大的關係?
我在Twitter的按鈕抓住這個陌生的網址了一個對象:此網址的最後一部分
我需要的「 html的」,這在例如前拿到最後一部分上面是:spriteslamdunk
這似乎%2Ftopics%2F後出現
不知道如何提取使用jQuery?
[編輯]看起來我必須用正則表達式來做?在這方面與正則表達式沒有太大的關係?
VAR答案= url.substring(url.lastIndexOf( 「%2F」)+ 3).replace( 「HTML」, 「」)
我猜你可能需要使用正則表達式"(\w+)\.html$"
(\ w +)在他想保留的部分之前包含2F部分(2Fspriteslamdunk)。 – DaveS 2012-02-23 16:12:17
@DaveS我同意。如果情況總是如此,他總是可以'substring(2)'。 – 2012-02-23 16:25:22
您想在.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.
http://xkcd.com/208/ – talnicolas 2012-02-23 16:06:27
你想要做基本的字符串操作...你爲什麼要涉及jQuery? – Quentin 2012-02-23 16:08:20