当我尝试在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