Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 280
  • Last Modified:

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

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
hankknight
Asked:
hankknight
1 Solution
 
Terry WoodsIT GuruCommented:
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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now