【发布时间】:2016-08-10 16:00:14
【问题描述】:
有一种情况,我有一个包含多个链接字符串的字符串,如下所示。我需要用实际链接替换链接,例如:<a href=”http://www.testing.com” target=_blank>http://www.asdfasd.com</a>
示例文本:
http://www.testing.com
Simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap https://asdfasdf.com
我正在使用以下函数来获取出现“http”的所有位置的索引,但想知道是否有更简单的方法。
indexes(comments.Answers, "http");
function indexes(source, find) {
var result = [];
for (var i = 0; i < source.length; ++i) {
// If you want to search case insensitive use
// if (source.substring(i, i + find.length).toLowerCase() == find) {
if (source.substring(i, i + find.length) == find) {
result.push(i);
}
}
console.log("result ", result);
return result;
}
【问题讨论】:
-
您提供的源文本中没有
http的实例。你能更清楚你想要做什么吗?看起来 RegEx 可能是一个可能的解决方案。 -
是的,为什么不干脆做
str.replace(/www.testing.com/gi, '<a href="$1">$1</a>')? -
我不知道网址是什么。文本由客户端提供,需要自动格式化 URL
标签: javascript string lodash