當我嘗試在https://stackoverflow.com/
之類的文本框中粘貼url時,它不會自動轉換爲超鏈接。如何在粘貼時自動將url轉換爲超鏈接
我試過使用正則表達式,這是我之前詢問的Question。我在這個問題中使用的函數可以正常工作,但實際上它會替換所有鏈接,包括標籤中的鏈接(IMG,現有的A HREF)。
我不想使用regx如果我使用regx轉換時發生,當我點擊任何提交或保存按鈕。
**當用戶粘貼在文本框中輸入一個URL應該自動轉換的任何鏈接的超鏈接****
我已經嘗試過這種使用至REGx
例如:
what = "<span>In the task system, is there a way to automatically have any site/page URL or image URL be hyperlinked in a new window?</span><br><br><span>So If I type or copy http://www.stackoverflow.com/ for example anywhere in the description, in any of the internal messages or messages to clients, it automatically is a hyperlink in a new window.</span><br><a href="http://www.stackoverflow.com/">http://www.stackoverflow.com/</a><br> <br><span>Or if I input an image URL anywhere in support description, internal messages or messages to cleints, it automatically is a hyperlink in a new window:</span><br> <span>https://static.doubleclick.net/viewad/4327673/1-728x90.jpg</span><br><br><a href="https://static.doubleclick.net/viewad/4327673/1-728x90.jpg">https://static.doubleclick.net/viewad/4327673/1-728x90.jpg</a><br><br><br><span>This would save us a lot time in task building, reviewing and creating messages.</span>
Test URL's
http://www.stackoverflow.com/
https://stackoverflow.com/
https://stackoverflow.com/
www.stackoverflow.com
//stackoverflow.com/
<a href='https://stackoverflow.com/'>https://stackoverflow.com/</a>";
我試過這個代碼
function Linkify(what) {
str = what; out = ""; url = ""; i = 0;
do {
url = str.match(/((https?:\/\/)?([a-z\-]+\.)*[\-\w]+(\.[a-z]{2,4})+(\/[\w\_\-\?\=\&\.]*)*(?![a-z]))/i);
if(url!=null) {
// get href value
href = url[0];
if(href.substr(0,7)!="http://") href = "http://"+href;
// where the match occured
where = str.indexOf(url[0]);
// add it to the output
out += str.substr(0,where);
// link it
out += '<a href="'+href+'" target="_blank">'+url[0]+'</a>';
// prepare str for next round
str = str.substr((where+url[0].length));
} else {
out += str;
str = "";
}
} while(str.length>0);
return out;
}
fiddle無法運作
是否有可能將其自動轉換,當我們在文本框中粘貼URL就像我們在棧上的流動得到我可以有一些例子嗎?
謝謝。
你應該張貼,你有什麼第一次嘗試.. ! {CODE} –
將網址粘貼到哪裏? – ZiNNED
@RajaprabhuAravindasamy我試過在這個問題中使用正則表達式http://stackoverflow.com/questions/23759302/how-to-detect-links-with-out-anchor-element-in-a-plain-text我提到,在上面的問題 – dhee