-1

I just need to find a regular expression for the following:

I have some content in div tag, that includes lot of anchor links in it. So my task is to find anchor links with href as format of "components/showdoc.aspx?docid=" And then add onclick event for that anchor link only, leave the rest of the anchor links.

<div id="content" runat="server">
    <a href="www.xxxx.com/components/showdoc.aspx?docid=100">test doc</a>
</div>

This expression gives and add target to it.

RegEx.Replace(inputString, "<(a)([^>]+)>", "<$1 target=""_blank""$2>")

Thanks

Mahmoud Gamal
  • 78,257
  • 17
  • 139
  • 164
user1542851
  • 21
  • 1
  • 6
  • 3
    http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 The
    cannot hold it is too late.
    – Mark Broadhurst Aug 24 '12 at 15:45
  • 1
    your onclick event is client-side, correct? would a javascript solution be acceptable? – RYFN Aug 24 '12 at 15:48
  • I agree with @Zeus. I'd use jQuery to do this. – JamieSee Aug 24 '12 at 16:00
  • Ia mthinking to do it this way:$('a[href*="components/showdoc.aspx?docid="]').on('click', myClickFunction); but my questions is how to access that document id in onclick function? – user1542851 Aug 24 '12 at 17:19

2 Answers2

0

Don't use regex to parse html, it's evil.

You could use the HTML Agility Pack, it even has a nice NuGet Package.

Alternatively, you could do this on the client side with a single line of jQuery:

$('a[href*="components/showdoc.aspx?docid="]').on('click', myClickFunction);

This is making use of the Attribute Contains Selector.

If you want to find the docid in your click function, you could write something like this in your click function:

function myClickFunction(e){
    var href = $(this).attr('href');
    var docId = href.split('=')[1];
    alert(docId);
}

Note that this assumes there's only ever one query string value, if you wanted to make this more robust you could do something like in this answer: https://stackoverflow.com/a/1171731/21200

Community
  • 1
  • 1
RYFN
  • 2,939
  • 1
  • 29
  • 40
  • this makes sense, but is there any way to get the docid from that url and use it in onclick function? – user1542851 Aug 24 '12 at 17:01
  • @user1542851, I've updated my answer to show how you can get the href of the link you've clicked on, and then find the query string value. – RYFN Aug 28 '12 at 08:17
0

If you are looking to make permanent changes to your HTML file, first manage your HTML parsing by loading it into a System.Windows.Forms.WebBrowser control. From there you can perform DOM-like modifications to the HTML without the dangerous repercussions of parsing corruption that can be caused by performing Regex.Replace on the raw file. (Apparently RegEx + HTML is a serious issue for some).

So first in your code you would:

WebBrowser myBrowser = new WebBrowser();
myBrowser.URL = @"C:\MyPath\MyFile.HTML";
HtmlElement myDocBody = myBrowser.Document.Body;

Then you can navigate through your document body, seeking out your div tag and looking for your anchor tags by using the HtmlElement.Id property and HtmlElement.GetAttribute method.

Note: feel free to still use RegEx matching on the URL strings but only after extracting them from a GetAttribute("href") method.

To add the onClick method, simply invoke the HtmlElement.SetAttribute method.

When you have finished all your modifications, save the changes by writing the WebBrowser.DocumentText to file.

Here is a reference: http://msdn.microsoft.com/en-us/library/system.windows.forms.htmlelement.aspx

nicholas
  • 2,969
  • 20
  • 39