?
Solved

Help with Regular expressions

Posted on 2007-08-07
19
Medium Priority
?
253 Views
Last Modified: 2010-04-15
Hi there

Wonder if you can help me?

I have this line of code in Javascript

var newHTML = HTMLCode.replace(/(target=["'](new|_blank).*?<\/a>)/gi,"$1"+insImg);

That line converts a

<a title='target this' href='http://www.google.com' target='_blank'>http://www.google.com</a>

To

<a title='target this' href='http://www.google.com' target='_blank'>http://www.google.com</a>&nbsp;<img title="" height="10" alt="" hspace="0" src="/image/openNewWindow.gif" width="10" border="0" />&nbsp;


Can someone please help me in coverting

var newHTML = HTMLCode.replace(/(target=["'](new|_blank).*?<\/a>)/gi,"$1"+insImg);

to C#?

Thanks
Mousemat24
0
Comment
Question by:mousemat24
  • 10
  • 5
  • 4
19 Comments
 
LVL 6

Accepted Solution

by:
gjutras earned 1000 total points
ID: 19644915
 // Regex search and replace
   string        stringToAdd = @">&nbsp;<img title='' height="10" alt='' hspace='0' src='/image/openNewWindow.gif' width='10' border='0' />&nbsp;";
    RegexOptions   options = RegexOptions.None;
    Regex          regex = new Regex(@"(target=[""'](new|_blank).*?<\/a>)", options);
    string         input = @"the quick brown fox jumped over the lazy dog";
    string         replacement = @"$!"+stringToAdd;
    string         result = regex.Replace(input, replacement);
0
 

Author Comment

by:mousemat24
ID: 19645360
gjutras

Thanks for answering, but how does it work if the input is all in quotes i.e.

<a title="target this" href="http://www.google.com" target="_blank">http://www.google.com</a>

Thanks
0
 
LVL 6

Expert Comment

by:gjutras
ID: 19645408
you can either do a replace of " to ' which still makes valid html
or
on the input string use the  c# syntax of "" to represent a "
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 6

Expert Comment

by:gjutras
ID: 19645416
I just noticed a cut and paste error
string        stringToAdd = @">&nbsp;<img title='' height="10" alt='' hspace='0' src='/image/openNewWindow.gif' width='10' border='0' />&nbsp;";
shoudl be
string        stringToAdd = @"&nbsp;<img title='' height="10" alt='' hspace='0' src='/image/openNewWindow.gif' width='10' border='0' />&nbsp;";
0
 
LVL 6

Expert Comment

by:gjutras
ID: 19645422
and one more miss
make that
string        stringToAdd = @"&nbsp;<img title='' height='10' alt='' hspace='0' src='/image/openNewWindow.gif' width='10' border='0' />&nbsp;";
0
 
LVL 6

Expert Comment

by:gjutras
ID: 19645435
sorry, I work on too many languages "" is vb,  c# is \" to represent a "
0
 
LVL 6

Expert Comment

by:gjutras
ID: 19645465
RegexOptions   options = RegexOptions.IgnoreCase;
I forgot to ignore case which you were doing in your original regex.
0
 

Author Comment

by:mousemat24
ID: 19645546
gjutras
I've done this

but Im getting errors

string stringToAdd = @"&nbsp;<img title=\"\" height=\"10\" alt=\"\" hspace=\"0\" src=\"/image/openNewWindow.gif\" width=\"10\" border=\"0\" />&nbsp;";   // ; expected
   
      RegexOptions   options = RegexOptions.IgnoreCase;
      Regex          regex = new Regex(@"(target=[\"'](new|_blank).*?<\/a>)", options); // ) expected

      string         input = @"the quick brown fox jumped over the lazy dog";
      string         replacement = @"$!"+stringToAdd;
      string         result = regex.Replace(input, replacement);



As you can see I've also put

regex = new Regex(@"(target=[\"']    hoping that will sort out the quotes, but I still get errors. do you have any idea why this isnt working?

Thanks
0
 
LVL 6

Expert Comment

by:gjutras
ID: 19645624
try dropping the @ symbol (my bad) I also mad one slight change in the regex near /a
            string stringToAdd = "&nbsp;<img title=\"\" height=\"10\" alt=\"\" hspace=\"0\" src=\"/image/openNewWindow.gif\" width=\"10\" border=\"0\" />&nbsp;";   // ; expected
   
      RegexOptions   options = RegexOptions.IgnoreCase;
      Regex          regex = new Regex("(target=[\"'](new|_blank).*?<\\/a>)", options);
0
 

Author Comment

by:mousemat24
ID: 19645901
gjutras

I think its almost there, when I run the code below:

string stringToAdd = "&nbsp;<img title=\"\" height=\"10\" alt=\"\" hspace=\"0\" src=\"/image/openNewWindow.gif\" width=\"10\" border=\"0\" />&nbsp;";   // ; expected

      RegexOptions options = RegexOptions.IgnoreCase;
      Regex regex = new Regex("(target=[\"'](new|_blank).*?<\\/a>)", options);
      string input = "asdasdasdd <a href=\"http://www.google.com\" target=\"_blank\">asdasdsad</a> asdasdasdd<br /><a href=\"http://www.yahoo.com\" target=\"new\">asdasdas</a> sadsadsadadd <a href=\"http://www.itv.com\" target=\"_self\">asdddddd</a>";
      string replacement = @"$!"+stringToAdd;
      string result = regex.Replace(input, replacement);

////////////////////////////////

but the result is equal to

asdasdasdd <a href="http://www.google.com" $!&nbsp;<img title="" height="10" alt="" hspace="0" src="/image/openNewWindow.gif" width="10" border="0" />&nbsp; asdasdasdd<br /><a href="http://www.yahoo.com" $!&nbsp;<img title="" height="10" alt="" hspace="0" src="/image/openNewWindow.gif" width="10" border="0" />&nbsp; sadsadsadadd <a href="http://www.itv.com" target="_self">asdddddd</a>

This is wrong, as it should be

asdasdasdd <a href="http://www.google.com" target="_blank">asdasdsad</a> <img title="" height="10" alt="" hspace="0" src="/images/business/address_book.png" width="10" border="0" />&nbsp;asdasdasdd<br />
<a href="http://www.yahoo.com" target="_blank">asdasdas</a> <img title="" height="10" alt="" hspace="0" src="/images/business/address_book.png" width="10" border="0" />&nbsp;sadsadsadadd <a href="http://www.itv.com" target="_self">asdddddd</a>

Hope that makes sense? if not, please let me know
Mousemat24
0
 
LVL 64

Assisted Solution

by:Fernando Soto
Fernando Soto earned 1000 total points
ID: 19646133
Hi mousemat24;

This sample code should do the trick.

    // Test string
    string input = "<a title='target this' href='http://www.google.com' target='_blank'>http://www.google.com</a>";

    string stringToAdd = "&nbsp;<img title=\"\" height=\"10\" alt=\"\" hspace=\"0\" src=\"/image/openNewWindow.gif\" width=\"10\" border=\"0\" />&nbsp;";
    string replacement = "$1" + stringToAdd;
    Regex regex = new Regex("(?i)(target=[\"'](new|_blank).*?</a>)");
    string result = regex.Replace(input, replacement);

    // Display the result string
    MessageBox.Show(result);


Fernando
0
 
LVL 6

Expert Comment

by:gjutras
ID: 19646137
string replacement = @"$!"+stringToAdd;
should be
string replacement = @"$1"+stringToAdd;
0
 
LVL 6

Expert Comment

by:gjutras
ID: 19646225
and my \\/a should be /a in the regex.  javascript needed \/ to represent / but c# doesn't need \ to escape it.
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 19646616
Hi mousemat24;;

If you have any questions about my last post please let me know.
0
 

Author Comment

by:mousemat24
ID: 19649853
Hi guys, both your attempts worked, but Im not to sure how I should split the points, I want to give both of you guys points, as you helped me BIG TIME!!

Would you guys be happy if I split the points 250/250?

Thanks for helping me out again
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 19649883
That is fine by me. I am glad it worked out for you. ;=)
0
 
LVL 6

Expert Comment

by:gjutras
ID: 19650825
same here, that's fine.
0
 

Author Comment

by:mousemat24
ID: 19652614
oooh man, this Assisted Solution / Accepted Solution is messed up guys, sorry
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 19653787
Not a problem, have a great day and thanks. ;=)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses
Course of the Month14 days, left to enroll

809 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