Solved

Convert URL to Hyperlink

Posted on 2003-12-02
9
2,630 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

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…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…
Suggested Courses

617 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