【发布时间】:2014-07-08 16:27:28
【问题描述】:
如果用户在文本框中输入他的文本并保存它,然后再添加更多文本,他可以编辑该文本并在需要时保存它。
首先,如果用户输入带有一些链接的文本,我会检测到它们并将任何超链接转换为在新选项卡中链接。其次,如果用户想要添加更多文本和链接,他点击编辑并添加它们并保存它此时我必须忽略已经与锚按钮超链接的链接
请帮助和建议
例如:
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/
http://stackoverflow.com/
https://stackoverflow.com/
www.stackoverflow.com
//stackoverflow.com/
<a href='http://stackoverflow.com/'>http://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;
}
请帮忙 谢谢。
【问题讨论】:
-
能提供一个js小提琴吗?
-
你为什么不直接使用
str.replace(/yourRegExp/, '')? -
@Dwza 替换/匹配没关系我怎么能忽略已经用锚按钮超链接的链接
-
这是我的另一个question 并得到了答案
标签: javascript jquery html