Link to home
Start Free TrialLog in
Avatar of mousemat24
mousemat24

asked on

Help with Regular expressions

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
ASKER CERTIFIED SOLUTION
Avatar of gjutras
gjutras

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mousemat24
mousemat24

ASKER

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
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 "
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;";
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;";
sorry, I work on too many languages "" is vb,  c# is \" to represent a "
RegexOptions   options = RegexOptions.IgnoreCase;
I forgot to ignore case which you were doing in your original regex.
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
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);
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
SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
string replacement = @"$!"+stringToAdd;
should be
string replacement = @"$1"+stringToAdd;
and my \\/a should be /a in the regex.  javascript needed \/ to represent / but c# doesn't need \ to escape it.
Hi mousemat24;;

If you have any questions about my last post please let me know.
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
That is fine by me. I am glad it worked out for you. ;=)
same here, that's fine.
oooh man, this Assisted Solution / Accepted Solution is messed up guys, sorry
Not a problem, have a great day and thanks. ;=)