Question: I am using the following Regular Expresion to remove html tags from a string. It works except I leave the closing tag. If I attempt to remove: `<a href="blah">blah</a>` it leaves the `<a/>`.

I do not know Regular Expression syntax at all and fumbled through this. Can someone with RegEx knowledge please provide me with a pattern that will work.

Here is my code:

```
  string sPattern = @"<\/?!?(img|a)[^>]*>";
  Regex rgx = new Regex(sPattern);
  Match m = rgx.Match(sSummary);
  string sResult = "";
  if (m.Success)
   sResult = rgx.Replace(sSummary, "", 1);

```

I am looking to remove the first occurence of the `<a>` and `<img>` tags.

Answer: 
If all you're trying to do is remove the tags (and not figure out where the closing tag is), I'm really not sure why people are so fraught about it.

This Regex seems to handle anything I can throw at it:

`<([\w\-/]+)( +[\w\-]+(=(('[^']*')|("[^"]*")))?)* *>`

To break it down:

* `<([\w\-/]+)` - match the beginning of the opening or closing tag. if you want to handle invalid stuff, you can add more here
* `( +[\w\-]+(=(('[^']*')|("[^"]*")))?)*` - this bit matches attributes [0, N] times (`*` at then end)
	+ `+[\w\-]+` - is space(s) followed by an attribute name
	+ `(=(('[^']*')|("[^"]*")))?` - not all attributes have assignment (`?`)
		- `('[^']*')|("[^"]*")` - of the attributes that do have assignment, the value is a string with either single or double quotes. It's not allowed to skip over a closing quote to make things work
* `*>` - the whole thing ends with any number of spaces, then the closing bracket

Obviously this will mess up if someone throws super invalid html at it, but it works for anything valid I've come up with yet. Test it out here:

```js
const regex = /<([\w\-/]+)( +[\w\-]+(=(('[^']*')|("[^"]*")))?)* *>/g;

const byId = (id) => document.getElementById(id);

function replace() {
console.log(byId("In").value)
  byId("Out").innerText = byId("In").value.replace(regex, "CUT");
}
```

```html
Write your html here: <br>
<textarea id="In" rows="8" cols="50"></textarea><br>
<button onclick="replace()">Replace all tags with "CUT"</button><br>
<br>
Output:
<div id="Out"></div>
```
Remove image from the string, using a regular expression in c# (image search performed by image id)

```
string PRQ=<td valign=\"top\" style=\"width: 400px;\" align=\"left\"><img id=\"llgo\" src=\"http://test.Logo.png\" alt=\"logo\"></td>

var regex = new Regex("(<img(.+?)id=\"llgo\"(.+?))src=\"([^\"]+)\"");

PRQ = regex.Replace(PRQ, match => match.Groups[1].Value + "");

```