1

Say I want to convert these strings:

www.myexample.com and http://www.myexample.com
into:

<a href='http://www.myexample.com'>http://www.myexample.com</a>

using Regex.Replace

I've come up with this:

Regex.Replace(string, pattern, "<a href=\"$&\">$&</a>")

My problem is that I don't know how to check if the matched string $& starts with http:// and adds it if necessary.

Any ideas?

1
  • What's the pattern you've been using? Commented Aug 10, 2013 at 12:08

1 Answer 1

1

If you don't have to consider https or things like that, you could maybe use this:

Regex.Replace(string, @"(?:http://)?(.+)", "<a href=\"http://$1\">http://$1</a>")
Sign up to request clarification or add additional context in comments.

1 Comment

If you wanted to consider the https possibility, then change the http: to https?:

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.