-1

I'm trying to write a regular expression to see if a string contains any of the typical table tags:

<table></table>
<td></td>
<th></th>
<tr></tr>
<thead></thead>
<tfoot></tfoot>
<tbody></tbody>

Along with tags that may contain other attributes e.g:

<table border="1">

I've come up with this so far, however, it matches <br /> tag and I'm not sure why:

/<\/?[table|td|th|tr|tfoot|thead|tbody]{1,}>?/

http://www.rexfiddle.net/20Xtqka

keeg
  • 3,990
  • 8
  • 49
  • 97
  • It's matching br because of the square brackets and the "tr". You are just saying, match at least one of these characters with the square brackets – jpmuc Nov 04 '13 at 21:57

1 Answers1

0

Regular expressions use parentheses, not square brackets, to group things. A set of characters inside square brackets matches any of those characters.

/<\/?(table|td|th|tr|tfoot|thead|tbody)+>?/

When you want to match 1 or more of something, use + rather than {1,}.

Barmar
  • 741,623
  • 53
  • 500
  • 612