将字符串中的 URL 替换为包含匹配 URL 的 HTML 字符串的车把助手?
Handlebar Helper to Replace URL in String with HTML String Containing the Matching URL?
There is a similar question, but the result is comparatively specific..
我有一串看起来像这样的段落行(从文本区域保存):
"Here are some links\n\nhttp://www.example.com is one of them\n\nand http://duckduckgo.com is another"
我如何将每个 URL、http://www.example.com
和 http://duckduckgo.com
替换为:
<a href="http://www.example.com" target="_blank">www.example.com</a>
and
<a href="http://duckduckgo.com" target="_blank">duckduckgo.com</a>
这样所有 URL 都呈现为链接,其文本不包括 http://..
"Here are some links\n\n<a href="http://www.example.com" target="_blank">www.example.com</a> is one of them\n\nand <a href="http://duckduckgo.com" target="_blank">duckduckgo.com</a> is another"
渲染:
这里有一些链接
www.example.com就是其中之一
并且duckduckgo.com是另一个
来自车把助手..
Handlebars.registerHelper('linkingplaintext', function(plaintext) {
// some code to replace the URLs with their equivalent links
return new Handlebars.SafeString(linkedplainText);
});
这适用于您提供的示例字符串:
var text = "Here are some links\n\nhttp://www.example.com\n\nLorem ipsum dolor\n\nhttp://www.example.com"
var urls = text.split('\n').filter(function(v) {
return v.indexOf('http') > -1;
});
$.each(urls, function(i,v) {
$('<a></a>').attr('href', v).html(v).appendTo('body');
$('body').append('<br />');
});
示例 JSFiddle。
--编辑--
现在您已经更新了问题,这里是您可以使用的车把助手:
Handlebars.registerHelper('wrapURL', function(str) {
str = Handlebars.Utils.escapeExpression(str);
var matches = str.match(/http\S+/);
var wrapped = matches.map(function(v, i, a) {
return '<a href="' + v + '">' + v + '</a>';
});
for (var i = 0; i < matches.length; i++) {
str = str.replace(matches[i], wrapped[i]);
}
return new Handlebars.SafeString(str)
});
以及 JSFIddle 上的工作示例。
There is a similar question, but the result is comparatively specific..
我有一串看起来像这样的段落行(从文本区域保存):
"Here are some links\n\nhttp://www.example.com is one of them\n\nand http://duckduckgo.com is another"
我如何将每个 URL、http://www.example.com
和 http://duckduckgo.com
替换为:
<a href="http://www.example.com" target="_blank">www.example.com</a>
and
<a href="http://duckduckgo.com" target="_blank">duckduckgo.com</a>
这样所有 URL 都呈现为链接,其文本不包括 http://..
"Here are some links\n\n<a href="http://www.example.com" target="_blank">www.example.com</a> is one of them\n\nand <a href="http://duckduckgo.com" target="_blank">duckduckgo.com</a> is another"
渲染:
这里有一些链接
www.example.com就是其中之一
并且duckduckgo.com是另一个
来自车把助手..
Handlebars.registerHelper('linkingplaintext', function(plaintext) {
// some code to replace the URLs with their equivalent links
return new Handlebars.SafeString(linkedplainText);
});
这适用于您提供的示例字符串:
var text = "Here are some links\n\nhttp://www.example.com\n\nLorem ipsum dolor\n\nhttp://www.example.com"
var urls = text.split('\n').filter(function(v) {
return v.indexOf('http') > -1;
});
$.each(urls, function(i,v) {
$('<a></a>').attr('href', v).html(v).appendTo('body');
$('body').append('<br />');
});
示例 JSFiddle。
--编辑--
现在您已经更新了问题,这里是您可以使用的车把助手:
Handlebars.registerHelper('wrapURL', function(str) {
str = Handlebars.Utils.escapeExpression(str);
var matches = str.match(/http\S+/);
var wrapped = matches.map(function(v, i, a) {
return '<a href="' + v + '">' + v + '</a>';
});
for (var i = 0; i < matches.length; i++) {
str = str.replace(matches[i], wrapped[i]);
}
return new Handlebars.SafeString(str)
});
以及 JSFIddle 上的工作示例。