Solved

Convert URL to Hyperlink

Posted on 2003-12-02
9
2,602 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
 
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

760 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

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now