D
D
deleted-mezhevikin2011-04-01 09:13:51
JavaScript
deleted-mezhevikin, 2011-04-01 09:13:51

Autodetect javascript link

Does anyone have a ready-made solution for auto-detection of a link in a string in javascript?

That is, given the line:

Вот ссылка http://site.com


We get:
Вот ссылка <a href="http://site.com" target="_blank">http://site.com</a>


I'm trying to find a link through a regular expression, but it returns null:

<script>
  var str = 'Вот ссылка http://site.com';	
  var test  = str.match("/^((http|https):\/\/)?(www.)?([a-z0-9-]+\.)+[a-z]{2,6}$/");
  alert(test);
</script>

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
shsmad, 2011-04-01
@deleted-mezhevikin

Change to:

var test = str.match("((http|https):\/\/)?(www.)?([a-z0-9-]+\.)+[a-z]{2,6}");

D
Desiderata, 2011-04-01
@Desiderata

then like this:

found = /((http|https):\/\/(www\.)?[a-zа-я0-9-]+\.[a-zа-я0-9-]{2,6})/i.exec("текстовая ссылка http://test911.рф");
document.write('found: ' + found);

A
Anatoly, 2011-04-01
@taliban

var str = " sub.demo.com "; Just wonderful.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question