0

Good afternoon

I am testing my Regular Expression on regex101.com site. Regular Expression:

<button[^<]*>.*[\s\S]*<\/button>|
<iframe[^<>]*\/>|
<tr>[^<td>]*<\/tr>

Test Text

<table width="30px">
<P>Teste</p><iframe src="teste" /><p>Test</p>
<div><span>Teste Regex</span></div>
<p>Test</p><button onclick="myFunction()">Click me</button>
<table>
<tr><td>34</td></tr>>
</table>

Why only the <button[^<]*>.*[\s\S]*<\/button> code is matched ? If i clear this part of the regular expression and stay only with this:

<iframe[^<>]*\/>|
<tr>[^<td>]*<\/tr>

Only the <iframe.. is matched

Why is this happen and how can I fix it ?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
tt0686
  • 1,771
  • 6
  • 31
  • 60
  • 1
    If this is any kind of production system or has any security responsibility I strongly recommend you dont use regex to parse/clean HTML - http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – Sam Greenhalgh Nov 20 '14 at 16:10
  • 1
    Why you shouldn't try to parse HTML with regular expressions: http://stackoverflow.com/questions/701166/can-you-provide-some-examples-of-why-it-is-hard-to-parse-xml-and-html-with-a-reg – Shmiddty Nov 20 '14 at 16:11
  • The users of the application can make Copy and Paste to a rick Text Editor on our apllication, some times the users select all text from a page , and inadvertently copy buttons , javascript etc. When we show on otjer page the text inputed by the users , this text throw erros because contain those button and iframes codes – tt0686 Nov 20 '14 at 17:00

1 Answers1

1

You have to remove the new lines before your OR and also set the G (global flag).

Working demo

enter image description here

Federico Piazza
  • 30,085
  • 15
  • 87
  • 123