Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Convert URL to Hyperlink

Posted on 2003-12-02
9
Medium Priority
?
2,638 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
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…

715 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