2012-04-26 22 views
0

我檢查上述所有答案,看我的得到的回答是,請不要標記這個作爲重複單獨的網頁鏈接到一個JS數組

在此我建立有一個div

應用
<div class="paragraphlink"> </div> 

我允許用戶輸入這樣

萊科寧任何文本是兩個方程式中車手http://www.formula1.com誰把它做成了福布斯雜誌的名人100名單,另一個是費爾南多·阿隆索http://www.google.com。他是第36屆福布斯雜誌的http://www.forbesmagazine.com

如果您發現有存在於上述第網址。你能幫我用JavaScript代碼從上面的段中提取所有的URL嗎?

+0

這是不是*重複約一百萬個問題?你在哪裏遇到問題,爲什麼其他解決方案不適合你? – JJJ 2012-04-26 15:14:59

回答

0

可以RegExp對象

var reg = new RegExp(/http(s)?:\/\/[a-z0-9_-\.\/]+/ig); 

var html = "Räikkönen was among the two Formula One drivers http://www.formula1.com who made it into the Forbes magazine's The Celebrity 100 list, the other being Fernando Alonso http://www.google.com. He is 36th on Forbes magazine's http://www.forbesmagazine.com" 

html.match(reg); 

將返回的URL的數組。

+0

Thanks..this works excellent – Jay66online 2012-04-26 15:40:40

0

使用正則表達式

這樣

 /(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/g 

正則表達式匹配的URL

更多JavaScript的正則表達式檢查thisthis

0

您可以使用匹配功能對您文本提取網址爲例:

var str = "Räikkönen was among the two Formula One drivers http://www.formula1.com who made it into the Forbes magazine's The Celebrity 100 list, the other being Fernando Alonso http://www.google.com. He is 36th on Forbes magazine's http://www.forbesmagazine.com"; 

var urls = str.match(/http(s)?:\/\/[a-z0-9_-\.\/]+/ig); 
// now the var "urls" is in array with your URL 

但是正則表達式在這裏很簡單,你可以在很多網站上找到更好的。

+0

Thanks..this works excellent – Jay66online 2012-04-26 15:39:59