如何使用javascript检查文本中是否存在链接

how to check the existance of a link in a text using javascript

本文关键字:是否 存在 链接 文本 何使用 javascript 检查      更新时间:2023-09-26

可能重复:
检测url';使用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不起作用。我也想在变量中得到那个链接

请帮忙。感谢

尝试以下正则表达式

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

试试这个:

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