Solved

Convert URL to Hyperlink

Posted on 2003-12-02
9
2,620 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Video Tutorial help 2 51
Add Rows on a Table 8 53
Conformation code 4 36
Need help for active menu class depending of the page 4 29
In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
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…

738 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