I'm trying to implement a hashtag function in a web app to easily embed search links into a page. The issue is that I'm trying to do a replace on the hash marks so they don't appear in the HTML output. Since I'm also wanting to be able to also have hash marks in the output I can't just do a final Replace on the entire string at the end of processing. I'm going to want to be able to escape some hash marks like so \#1 is my answer and I'd find and replace the \# with just # but that is another problem that I'm not even ready for (but still thinking of).
This is what I have so far mocked up in a console app,
static void Main(string[] args)
{
Regex _regex = new Regex(@"(#([a-z0-9]+))");
string link = _regex.Replace("<p>this is #my hash #tag.</p>", MakeLink("$1"));
}
public static string MakeLink(string tag)
{
return string.Format("<a href=\"/Search?t={0}\">{1}</a>", tag.Replace("#", ""), tag);
}
The output being:
<p>this is <a href="/Search?t=#my">#my</a> hash <a href="/Search?t=#tag">#tag</a>.</p>
But when I run it with breaks while it's running MakeLink() it's string is displayed at "$1" in the debugger output and it's not replacing the hash's as expected.
Is there a better tool for the job than regex? Or can I do something else to get this working correctly?