【问题标题】:how to check the existance of a link in a text using javascript [duplicate]如何使用javascript检查文本中是否存在链接[重复]
【发布时间】:2012-03-30 21:36:03
【问题描述】:

可能重复:
detect url's in text with Javascript

我想检查文本中是否存在链接。 想要检查 http、https 和/或 www 链接。 我试过这个。

.replace(/(\w+):\/\/[\S]+(\b|$)/gim,'<a href="$&" class="my_link" target="_blank">$&</a>')
        .replace(/([^\/])(www[\S]+(\b|$))/gim,'$1<a href="http://$2" class="my_link" target="_blank">$2</a>');

http 和 https 工作的地方 但 www 不工作。 我也想在变量中获取该链接

请帮忙。 谢谢

【问题讨论】:

  • 你试过什么?你有一个具体的问题吗?任何特定的 JavaScript 框架?

标签: javascript regex


【解决方案1】:

试试下面的正则表达式

(https?://)?www\.[a-zA-Z0-9]{3,}\.[a-z]{2,4}

【讨论】:

  • 抱歉,这个不行。我使用单独的正则表达式完成了这项工作。 /(https?:\/\/)[a-zA-Z0-9]{3,}(\.[a-z]{2,4})*/g/www\.[a-zA-Z0-9]{3,}(\.[a-z]{2,4})*/g 感谢您的回复。
【解决方案2】:

试试这个:

function checkLink(text) {
    if (text.search('http') > 0 || text.search('https') > 0 || text.search('www.') > 0) {
        return true;
    } else {
        return false
    }
}

【讨论】:

  • 我认为这 1 行不通。因为如果文本中包含一个单词 http/https/www。它看起来像链接 na?
猜你喜欢
  • 2011-12-19
  • 2012-11-30
  • 1970-01-01
  • 1970-01-01
  • 2013-08-07
  • 1970-01-01
  • 2014-04-16
  • 2013-01-17
  • 2020-11-16
相关资源
最近更新 更多