更新时间:2023-02-23 11:07:43
一种解决方法是使用正则表达式.假设有什么输入,您可以做类似的事情
One way to approach this would be with the use of regular expressions. Assuming whatever input, you can do something like
var expression = /(https?:\/\/(?:www\.|(?!www))[^\s\.]+\.[^\s]{2,}|www\.[^\s]+\.[^\s]{2,})/gi;
var matches = input.match(expression);
然后,您可以使用 indexOf
for(match in matches)
{
var result = {};
result['link'] = matches[match];
result['startsAt'] = input.indexOf(matches[match]);
result['endsAt'] =
input.indexOf(matches[match]) + matches[match].length;
}
当然,您可能必须修改正则表达式本身才能满足您的特定需求.
Of course, you may have to tinker with the regular expression itself to suit your specific needs.
您可以在此小提琴