2

I have an xml which contains some html tags also. When a tag comes in, it breaks the page because it's a self closing tag. Something like:

<iframe width="420" height="315" src="//www.youtube.com/embed/6krfYKxJFqA" frameborder="0" />

I want to replace this and convert it to:

Can anyone provide a c# code with regex to do this. I tried doing:

tmp = tmp.Replace("(<iframe[^>]*)(\\s*/>)", "$1></iframe>");

and

tmp = new Regex(@"(<iframe[^>]*)(\\s*/>)").Replace(tmp, "$1></iframe>");

tmp is the xml containing lot of code + this iframe tag as string.

but with no result.

4
  • 2
    In the second regex, you don't need the double backslash as you are using @ Commented Feb 18, 2014 at 6:28
  • Bravo.. Thanks. That was the problem. Just for knowledge, what difference does @ cause? Commented Feb 18, 2014 at 6:35
  • Sure. Posted as an answer please accept. Commented Feb 18, 2014 at 6:39
  • These things can be harder than they look. Commented Jun 8, 2014 at 20:13

2 Answers 2

1

Try this as a match expression:

<iframe(.*?)(["\d\w\s])\/>

note that you can use http://regexpal.com/ to test regex, it's super convenient.

Sign up to request clarification or add additional context in comments.

Comments

0

In the second regex, you don't need the double backslash as you are using @.

Also, (<iframe[^>]*) also matches the last /, use the non-greedy ? operator: (<iframe[^>]*?)(\s*/>)

2 Comments

How do I completely remove the iframe tags if I want to?
@RossCooper Use <iframe[^>]*> and replace it with an empty string ""

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.