2011-11-06 22 views
-1

所以我有這樣的功能在這裏:檢查文本區域玲,並將其轉變成超級鏈接

function ShowMessage() 
{ 
    var themessege = document.getElementById("Form").textarea1.value; 
    var dat = new Date(); 
    var fileName = document.getElementById("theFile").value; 
    var image = '<img src="' + fileName + '"/>' + '<br>'; 
    document.getElementById("Form").textarea1.value = ""; 
    document.getElementById("Form").countdown.value = "160"; 
    document.getElementById("theFile").value = ""; 
    if (themessege==null || themessege=="") 
     { 
     alert("There is no text to submit, please fill out the text box"); 
     return false; 
     } 

    document.getElementById("blog").innerHTML = document.getElementById("blog").innerHTML + image + "Guest post: " + themessege + "<br />" + dat +"<br />"; 
} 

我可以從文本區域中的文本,以及圖像用戶上傳。我想知道如何將文本區域中的字符串劃分爲子項以檢查它們是以「www」還是「htt」開頭。

這是我到目前爲止已經寫的:

function linkify(inputText) { 
    var replaceText, replacePattern1, replacePattern2; 

    //URLs starting with http://, https:// 
    replacePattern1 =https; 
    replacedText = inputText.replace(replacePattern1, '<a href= ></a>'); 

    //URLs starting with "www." (without // before it, or it'd re-link the ones done above). 
    replacePattern2 = www.; 
    replacedText = replacedText.replace(replacePattern2, <a href="http: ></a>'); 
    var x = getelementbyid("blog"); 
    for(var i = 0;i < x.length;i++){ 
     if(blog.charAt(i) == replacePattern1){ 
     return replacedText; 
     } 
    } 
    else if(blog.charAt(i) == replacePattern2){ 
    return replacedText; 
    } 
} 

我知道charAt(i)只爲1個字母檢查....

大多數,我發現是在PHP的答案,我試圖找到一個使用JavaScript的解決方案。

回答

1

linkify函數沒有被正確寫入。試試這個。

function linkify(inputText) { 
var regUrl = /(http:[\d]{0,4}\/\/)?[a-zA-Z0-9\-\.]+\.(com|org|net|mil|edu|COM|ORG|NET|MIL|EDU)/; 

return inputText.replace(regUrl,'<a href="" />'); 
} 
; 
alert(linkify('string containing link www.google.com'));// output: string containing link <a href="" /> 
0

你的linkify函數全是錯誤的。所有變量都包含對不存在變量的引用!把引號放在他們周圍,使他們成爲字符串。

replacedText = inputText.replace(replacePattern1, '<a href= ></a>');是做什麼用的?

說inputText是'https://example.com',並假設你更正了你的變量,所以replacePattern1 === 'https' 你找到了https並將其替換。最終產品的外觀如何?

replacedText === '<a href= ></a>://example.com';

同樣的事情發生與第二替換模式。

此外,沒有什麼像getelementbyid。它是document.getElementById。 JavaScript區分大小寫。它返回一個帶有一個id的單個元素,所以你不能遍歷它。

請在嘗試編寫JavaScript代碼之前先學習JavaScript。 Here's a good resource to start。另請閱讀Douglas Crockford的JavaScript: The Good Parts


回答你的問題,這裏的代碼,你應該使用:

string.replace(/(https?:\/\/[^\s]+)/g, "<a href='$1'>$1</a>") 

https?意味着要麼HTTP或HTTPS。 [^\s]+意味着任何東西,但空白字符。 $1表示整個匹配的URL。 This is a good reference for Regular Expressions in JavaScript

這將允許https和http URL包含字母和數字,並將URL轉換爲鏈接。

Demo

+0

感謝,我努力學習JavaScript,我的網頁工作正常,我感謝你的可點擊的鏈接,但有我們可以將字符串轉換文本區域內的方式,我有到子使我可以添加if語句來替換以www和http/https開頭的字符串?這是我的主要問題... –

+0

我不明白你想說什麼。請舉例? –

+0

好吧,我有一個測試是用戶將輸入隨機文本,而他打字他輸入一個鏈接,例如www。google.com或任何,當他點擊提交鏈接www.google.com顯示爲正常文本,而不是一個可點擊的鏈接,即時通訊試圖找出一種方法來循環瀏覽textarea中的文本,找到以「www」或「hhtp」並將整個文本轉換爲可點擊的鏈接 我希望能夠清楚 –