-1

Need a regular expression to match a string contains table tag <TABLE and ends with > in between it may have any character other than alert word.

For example: With regular expression for following String(html code) 2nd line shouldn't match if it have alert word otherwise it should match 2nd line


 1. <DIV type="HEADER">
 2.      <TABLE border="0" cellpadding="7" cellspacing="0" width="655" onmouseover="alert('Table XSS')">
 3.          <COL width="313" />
 4.          <TBODY>
 5.             <TR valign="TOP">
 6.               <TD width="313">
 7.                       <P align="LEFT" style="margin-top: 0in">
 8.                         <IMG border="0" height="33" name="graphics3" src="/abc" width="74" />
 9.                       </P>
 10.                   </TD>
 11.               </TR>
 12.           </TBODY>
 13.       </TABLE> 
 14. </DIV>

I tried with this <table(?!alert)[\s\S]*[^>]*> regular expression but it's selecting 2nd line in above mentioned code.

Please help me to get appropriate regular expression to my requirement and thanks in advance.

Siva kumar
  • 215
  • 4
  • 13

1 Answers1

-1

As Tim Biegeleisen rightly commented, this is a job that should be handled by an HTML parser or using the DOM methods. But if you must use a regex, consider the following:

/<TABLE(?![^>]*alert)[^>]*>/g

See RegEx Demo

  1. <TABLE matches <Table (obviously).
  2. (?![^>]*alert) Uses a negative lookahead assertion to assert that alert does not appear before the next >.
  3. [^>]* Matches 0 or more non-> characters.
  4. > matches the closing > of the <TABLE> tag.
Booboo
  • 38,656
  • 3
  • 37
  • 60