5

I have a String from a tabel innerHTML where i need to remove all img tags using replace my Regex removes everthing after the first img tag my code at JSFiddle here

5
  • Do post what you have tried. Commented Aug 17, 2015 at 8:46
  • 1
    If you're getting this string from a table's .innerHTML property anyway - it's much better if you do this removal using DOM methods instead - don't try to do it with regex. Commented Aug 17, 2015 at 8:48
  • why is it better with dom? Commented Aug 17, 2015 at 8:49
  • 3
    because regex is bad for parsing html (except few rare cases) Commented Aug 17, 2015 at 8:50
  • you're performing an operation on a DOM string, why wouldn't use DOM methods - plus regex sux at HTML Commented Aug 17, 2015 at 8:51

1 Answer 1

14

Use non-greedy matching using .*?

var tmp = inner.replace(/<img .*?>/g,"REPLACED"); 

Regex Demo

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

2 Comments

Doing this kind of thing with regex is not reliable. For example, this would trip up on something like <img src='file.jpg' alt='Three > Two'>
@DalHundal I agree. I was just pointing out why Regex removes everthing after the first img tag

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.