Solved

ASP.NET / VB / REGEX: Automatically create links beginning with www at beginning

Posted on 2012-03-27
1
268 Views
Last Modified: 2012-03-27
I use the following code to make links clickable where the link begins with www.

input = RegularExpressions.Regex.Replace(input, "(\www\.\S+[^\s@,.""']+)", "<a href=""http://$1"">$1</a>")

Open in new window


The problem is that this becomes clickable links:
zwww.example.com
/www.example.com
>www.example.com

It should ONLY become a clickable link if it starts with www and does not have another charactor (other than whitespace)
0
Comment
Question by:hankknight
1 Comment
 
LVL 35

Accepted Solution

by:
Terry Woods earned 500 total points
ID: 37773742
Try this:
input = RegularExpressions.Regex.Replace(input, "(?<!\S)(\www\.\S+[^\s@,.""']+)", "<a href=""http://$1"">$1</a>")

By the way, the first w in www is backslashed in your pattern, which means it's matching any word character rather than just a literal w. It should probably be:
input = RegularExpressions.Regex.Replace(input, "(?<!\S)(www\.\S+[^\s@,.""']+)", "<a href=""http://$1"">$1</a>")
0

Featured Post

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.

Join & Write a Comment

As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

708 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

12 Experts available now in Live!

Get 1:1 Help Now