Solved

Convert URL to Hyperlink

Posted on 2003-12-02
9
2,613 Views
Last Modified: 2010-05-18
How can I use str.replace to convert a url in a string to a hyperlink?
0
Comment
Question by:chrisnetonline
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 31

Expert Comment

by:GwynforWeb
ID: 9862564
You can use this

<script>
URL="http://google.com"
link="<a href='" +URL+"'>" + URL +"</a>"
document.write(link)
</script>

0
 

Author Comment

by:chrisnetonline
ID: 9862604
i need it in the form of a regular expression
0
 
LVL 15

Expert Comment

by:Timbo87
ID: 9862622
Could you please clarify that?
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 10

Accepted Solution

by:
NetGroove earned 0 total points
ID: 9862687
Like this:

<script>

theText="Convert URL http://google.com to Hyperlink http://www.google.com or here."

alert(theText.replace(/(http\:\/\/[\w\.\-\?\!\&\=]+)/g,'<a href="$1">$1</a>'));

</script>



0
 

Author Comment

by:chrisnetonline
ID: 9862688
Such as the php solution that replaces urls in a string with a hyperlink:

ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]","<a href=\"\\0\" target=\"_blank\" title=\"\\0\">link</a>", $str);
0
 
LVL 15

Expert Comment

by:Timbo87
ID: 9862691
<script language="JavaScript" type="text/javascript">

var txt = "Welcome to http://www.google.com"

var lnk = ""
var st = txt.indexOf("http")
for(i=st; i<txt.length;i++)
if(txt.charAt(i)!=" ")lnk += txt.charAt(i)
var URL = "<a href=\"" + lnk + "\">" + lnk + "</a>"
document.write(URL)
</script>
0
 
LVL 15

Expert Comment

by:Timbo87
ID: 9862722
<script language="JavaScript" type="text/javascript">
var txt = "Welcome to http://www.google.com dsd"
var lnk = ""
var i
var st = txt.indexOf("http")
for(i=st; i<txt.length;i++)
if(txt.charAt(i)==" ") break
else lnk += txt.charAt(i)
var URL = "<a href=\"" + lnk + "\">" + lnk + "</a>"
var fp = txt.substring(0,st)
var ep = txt.substring(i,txt.length)
document.write(fp + URL + ep)
</script>
0
 

Author Comment

by:chrisnetonline
ID: 9862729
Thank you soooo much NetGroove
0
 
LVL 10

Expert Comment

by:NetGroove
ID: 9862739
You are welcome :)
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question