Scenario A:
Given the following string:
<a href="#" name="xy" onclick="alert('hello');">test</a>
… I would like to use Regular Expressions (Replace) to append some text (ie: alert(‘goodbye’); ) to the onclick attribute and end up with an anchor tag that looks as follows:
<a href="#" name="xy" onclick="alert('hello');alert('goodbye');">test</a>
Scenario B:
Given the following string(note the onclick attribute is missing):
<a href="#" name="xy">test</a>
… I would like to again use RegEx to determine that the onclick is missing and insert the attribute with some value (ie: alert(‘hello’); ) into the string ending up with an anchor tag that looks as follows:
<a href="#" name="xy" onclick="alert('hello');">test</a>
I am coding in c#
What Regular Expression do I use to accomplish this? Is using Regular Expressions a good approach to solving this problem?
Here’s an example using Regular Expressions, this assumes that strings will follow the formats posted in your example and also that there will be no more than one link HTML tag per string –