0

Possible Duplicate:
detect url's in text with Javascript

I want to check a text for the existence of links in it. want to check for http, https and/or www links. me tried this.

.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>');

where http and https are working but www is not working. i want to get that link in variable too

please help. thanks

Community
  • 1
  • 1
Krishna Raj
  • 846
  • 1
  • 12
  • 33

2 Answers2

0

Try this:

function checkLink(text) {
    if (text.search('http') > 0 || text.search('https') > 0 || text.search('www.') > 0) {
        return true;
    } else {
        return false
    }
}
MatuDuke
  • 4,997
  • 1
  • 21
  • 26
  • i think this 1 not gonna work. because if the text contains a word http/https/www. it will appear like link na? – Krishna Raj Mar 14 '12 at 12:21
0

Try the following regular expression

(https?://)?www\.[a-zA-Z0-9]{3,}\.[a-z]{2,4}
user160820
  • 14,866
  • 22
  • 67
  • 94
  • sorry, this one not works. me done this using separate regexps. `/(https?:\/\/)[a-zA-Z0-9]{3,}(\.[a-z]{2,4})*/g` and `/www\.[a-zA-Z0-9]{3,}(\.[a-z]{2,4})*/g` thanks for your reply. – Krishna Raj Mar 15 '12 at 07:36