LoginSignup
24

More than 5 years have passed since last update.

HTML5で省略できるタグにマッチする正規表現

Last updated at Posted at 2015-01-23

注意

  • 一部のパターンは未完成です

  • 本投稿には原文の引用を載せていますが、著者は英訳が出来ません。翻訳ミスの指摘などは、翻訳元の投稿にお願いします。

  • 本投稿の正規表現をPHPで利用する場合、UTF-8の文字列に対して使う場合にはu(PCRE_UTF8)オプションを指定してください。
    指定しない場合、文字列が破壊される恐れがあります

参考

前提

  • 以下の正規表現は、正当なHTMLで動作させることを想定しています。
    要素ではない<>などの文字は、&lt;&gt;に置換されている必要があります。

  • この正規表現では肯定的前方先読み(?=pattern)と否定的前方先読み(?!pattern)のパターンを利用しています。
    また、厳密版のパターンでは、制御文字の指定のためバイトコード表現\xhh(hは16進表現の文字)を利用しています。

  • PHPのpreg_match_all関数で検証を行っていますが、ミスが存在するかもしれません。利用する場合、予め検証を行って下さい。またミスが見つかった場合、指摘していただければありがたいです。

HTMLの正規表現

簡易版

要素の開始タグにマッチする正規表現(属性込み)(閉じタグの無い要素を含む)
<[0-9a-zA-Z]+(\s+[^\s"'>/=]+(\s*=\s*([^\s"'=><`]+|'[^']*'|"[^"]*"))?)*\s*/?>
要素の閉じタグにマッチする正規表現
</[0-9a-zA-Z]+\s*>
コメントにマッチする正規表現
<!--[\s\S]*?-->
空白文字にマッチする正規表現
\s
x要素の開始タグにマッチする正規表現(属性込み)(閉じタグの無い要素を含む)
<x(\s+[^\s"'>/=]+(\s*=\s*([^\s"'=><`]+|'[^']*'|"[^"]*"))?)*\s*/?>
x要素の閉じタグにマッチする正規表現
</x\s*>

厳密版

要素の開始タグにマッチする正規表現(属性込み)(閉じタグの無い要素を含む)
<[0-9a-zA-Z]+([ \t\n\f\r]+[^ \x00-\x1F\x7F"'>/=]+([ \t\n\f\r]*=[ \t\n\f\r]*([^ \t\n\f\r"'=><`]+|'[^\x00-\x08\x0B\x0E-\x1F\x7F']*'|"[^\x00-\x08\x0B\x0E-\x1F\x7F"]*"))?)*[ \t\n\f\r]*/?>
要素の閉じタグにマッチする正規表現
</[0-9a-zA-Z]+[ \t\n\f\r]*>
コメントにマッチする正規表現
<!--[\s\S]*?-->
空白文字にマッチする正規表現
[ \t\n\f\r]
x要素の開始タグにマッチする正規表現(属性込み)(閉じタグの無い要素を含む)
<x([ \t\n\f\r]+[^ \x00-\x1F\x7F"'>/=]+([ \t\n\f\r]*=[ \t\n\f\r]*([^ \t\n\f\r"'=><`]+|'[^\x00-\x08\x0B\x0E-\x1F\x7F']*'|"[^\x00-\x08\x0B\x0E-\x1F\x7F"]*"))?)*[ \t\n\f\r]*/?>
x要素の閉じタグにマッチする正規表現
</x[ \t\n\f\r]*>
注意
  • 制御文字の指定のため、バイトコード表現の[\x00-\x1F\x7F]を利用しています。このため、環境によっては正常に動作しない可能性があります。
  • 仕様では、属性値などにUnicodeにない文字は利用できませんが、本投稿の正規表現では考慮していません。Unicodeにない文字の正規表現なんてどう書けばいいのよ…?

参考

正規表現

<html>

An html element's start tag may be omitted if the first thing inside the html element is not a comment.

<html> はその最初の内容がコメントでなければ省略できる

簡易版
<html\s*>(?!<!--[\s\S]*?-->)
厳密版
<html[ \t\n\f\r]*>(?!<!--[\s\S]*?-->)

</html>

An html element's end tag may be omitted if the html element is not immediately followed by a comment.

</html> は直後にコメントが続かなければ省略できる

簡易版
</html\s*>(?!<!--[\s\S]*?-->)
厳密版
</html[ \t\n\f\r]*>(?!<!--[\s\S]*?-->)

<head>

A head element's start tag may be omitted if the element is empty, or if the first thing inside the head element is an element.

<head> は内容が空か、最初の内容が要素なら省略できる

簡易版
<head\s*>(?=</head\s*>|<[0-9a-zA-Z]+(\s+[^\s"'>/=]+(\s*=\s*([^\s"'=><`]+|'[^']*'|"[^"]*"))?)*\s*/?>)
厳密版
<head[ \t\n\f\r]*>(?=</head[ \t\n\f\r]*>|<[0-9a-zA-Z]+([ \t\n\f\r]+[^ \x00-\x1F\x7F"'>/=]+([ \t\n\f\r]*=[ \t\n\f\r]*([^ \t\n\f\r"'=><`]+|'[^\x00-\x08\x0B\x0E-\x1F\x7F']*'|"[^\x00-\x08\x0B\x0E-\x1F\x7F"]*"))?)*[ \t\n\f\r]*/?>)

</head>

A head element's end tag may be omitted if the head element is not immediately followed by a space character or a comment.

</head> は直後に空白文字かコメントがなければ省略できる

簡易版
</head\s*>(?!\s|<!--[\s\S]*?-->)
厳密版
</head[ \t\n\f\r]*>(?![ \t\n\f\r]|<!--[\s\S]*?-->)

<body>

A body element's start tag may be omitted if the element is empty, or if the first thing inside the body element is not a space character or a comment, except if the first thing inside the body element is a meta, link, script, style, or template element.

<body> は内容が空か、最初の内容が空白文字かコメントでなければ省略できるが、最初の要素が meta, link, script, style, template なら省略できない

簡易版
<body\s*>(?=</body\s*>|(?!\s|<!--[\s\S]*?-->|<(script|style|template)(\s+[^\s"'>/=]+(\s*=\s*([^\s"'=><`]+|'[^']*'|"[^"]*"))?)*\s*>|<(meta|link)(\s+[^\s"'>/=]+(\s*=\s*([^\s"'=><`]+|'[^']*'|"[^"]*"))?)*\s*/?>))
厳密版
<body[ \t\n\f\r]*>(?=</body[ \t\n\f\r]*>|(?![ \t\n\f\r]|<!--[\s\S]*?-->|<(script|style|template)([ \t\n\f\r]+[^ \x00-\x1F\x7F"'>/=]+([ \t\n\f\r]*=[ \t\n\f\r]*([^ \t\n\f\r"'=><`]+|'[^\x00-\x08\x0B\x0E-\x1F\x7F']*'|"[^\x00-\x08\x0B\x0E-\x1F\x7F"]*"))?)*[ \t\n\f\r]*>|<(meta|link)([ \t\n\f\r]+[^ \x00-\x1F\x7F"'>/=]+([ \t\n\f\r]*=[ \t\n\f\r]*([^ \t\n\f\r"'=><`]+|'[^\x00-\x08\x0B\x0E-\x1F\x7F']*'|"[^\x00-\x08\x0B\x0E-\x1F\x7F"]*"))?)*[ \t\n\f\r]*/?>))

</body>

A body element's end tag may be omitted if the body element is not immediately followed by a comment.

</body> は直後にコメントが続かなければ省略できる

簡易版
</body\s*>(?!<!--[\s\S]*?-->)
厳密版
</body[ \t\n\f\r]*>(?!<!--[\s\S]*?-->)

</li>

An li element's end tag may be omitted if the li element is immediately followed by another li element or if there is no more content in the parent element.

</li> は直後に li 要素が続くか、親要素にそれ以上内容がなければ省略できる

簡易版
</li\s*>(?=<li(\s+[^\s"'>/=]+(\s*=\s*([^\s"'=><`]+|'[^']*'|"[^"]*"))?)*\s*>|</[0-9a-zA-Z]+\s*>)
厳密版
</li[ \t\n\f\r]*>(?=<li([ \t\n\f\r]+[^ \x00-\x1F\x7F"'>/=]+([ \t\n\f\r]*=[ \t\n\f\r]*([^ \t\n\f\r"'=><`]+|'[^\x00-\x08\x0B\x0E-\x1F\x7F']*'|"[^\x00-\x08\x0B\x0E-\x1F\x7F"]*"))?)*[ \t\n\f\r]*>|</[0-9a-zA-Z]+[ \t\n\f\r]*>)

</dt>

A dt element's end tag may be omitted if the dt element is immediately followed by another dt element or a dd element.

</dt> は直後に dt, dd 要素が続けば省略できる

簡易版
</dt\s*>(?=<(dt|dd)(\s+[^\s"'>/=]+(\s*=\s*([^\s"'=><`]+|'[^']*'|"[^"]*"))?)*\s*>)
厳密版
</dt[ \t\n\f\r]*>(?=<(dt|dd)([ \t\n\f\r]+[^ \x00-\x1F\x7F"'>/=]+([ \t\n\f\r]*=[ \t\n\f\r]*([^ \t\n\f\r"'=><`]+|'[^\x00-\x08\x0B\x0E-\x1F\x7F']*'|"[^\x00-\x08\x0B\x0E-\x1F\x7F"]*"))?)*[ \t\n\f\r]*>)

</dd>

A dd element's end tag may be omitted if the dd element is immediately followed by another dd element or a dt element, or if there is no more content in the parent element.

</dd> は直後に dt, dd 要素が続くか、親要素にそれ以上内容がなければ省略できる

簡易版
</dd\s*>(?=<(dt|dd)(\s+[^\s"'>/=]+(\s*=\s*([^\s"'=><`]+|'[^']*'|"[^"]*"))?)*\s*>|</[0-9a-zA-Z]+\s*>)
厳密版
</dd[ \t\n\f\r]*>(?=<(dt|dd)([ \t\n\f\r]+[^ \x00-\x1F\x7F"'>/=]+([ \t\n\f\r]*=[ \t\n\f\r]*([^ \t\n\f\r"'=><`]+|'[^\x00-\x08\x0B\x0E-\x1F\x7F']*'|"[^\x00-\x08\x0B\x0E-\x1F\x7F"]*"))?)*[ \t\n\f\r]*>|</[0-9a-zA-Z]+[ \t\n\f\r]*>)

</p>

A p element's end tag may be omitted if the p element is immediately followed by an address, article, aside, blockquote, details, div, dl, fieldset, figcaption, figure, footer, form, h1, h2, h3, h4, h5, h6, header, hgroup, hr, main, menu, nav, ol, p, pre, section, table, or ul element, or if there is no more content in the parent element and the parent element is an HTML element that is not an a, audio, del, ins, map, noscript, or video element.

</p> は直後に address, article, aside, blockquote, details, div, dl, fieldset, figcaption, figure, footer, form, h1, h2, h3, h4, h5, h6, header, hgroup, hr, main, menu, nav, ol, p, pre, section, table, ul 要素が続くか、親要素が a, audio, del, ins, map, noscript, video 以外で親要素にそれ以上内容がなければ省略できる

簡易版
</p\s*>(?=<(address|article|aside|blockquote|details|div|dl|fieldset|figcaption|figure|footer|form|h1|h2|h3|h4|h5|h6|header|hgroup|main|menu|nav|ol|p|pre|section|table|ul)(\s+[^\s"'>/=]+(\s*=\s*([^\s"'=><`]+|'[^']*'|"[^"]*"))?)*\s*>|<hr(\s+[^\s"'>/=]+(\s*=\s*([^\s"'=><`]+|'[^']*'|"[^"]*"))?)*\s*/?>|</(?!(a|audio|del|ins|map|noscript|video)\s*>)[0-9a-zA-Z]+\s*>)
厳密版
</p[ \t\n\f\r]*>(?=<(address|article|aside|blockquote|details|div|dl|fieldset|figcaption|figure|footer|form|h1|h2|h3|h4|h5|h6|header|hgroup|main|menu|nav|ol|p|pre|section|table|ul)([ \t\n\f\r]+[^ \x00-\x1F\x7F"'>/=]+([ \t\n\f\r]*=[ \t\n\f\r]*([^ \t\n\f\r"'=><`]+|'[^\x00-\x08\x0B\x0E-\x1F\x7F']*'|"[^\x00-\x08\x0B\x0E-\x1F\x7F"]*"))?)*[ \t\n\f\r]*>|<hr([ \t\n\f\r]+[^ \x00-\x1F\x7F"'>/=]+([ \t\n\f\r]*=[ \t\n\f\r]*([^ \t\n\f\r"'=><`]+|'[^\x00-\x08\x0B\x0E-\x1F\x7F']*'|"[^\x00-\x08\x0B\x0E-\x1F\x7F"]*"))?)*[ \t\n\f\r]*/?>|</(?!(a|audio|del|ins|map|noscript|video)[ \t\n\f\r]*>)[0-9a-zA-Z]+[ \t\n\f\r]*>)

</rt>

An rt element's end tag may be omitted if the rt element is immediately followed by an rt or rp element, or if there is no more content in the parent element.

</rt> は直後に rt, rp 要素が続くか、親要素にそれ以上内容がなければ省略できる

簡易版
</rt\s*>(?=<(rt|rp)(\s+[^\s"'>/=]+(\s*=\s*([^\s"'=><`]+|'[^']*'|"[^"]*"))?)*\s*>|</[0-9a-zA-Z]+\s*>)
厳密版
</rt[ \t\n\f\r]*>(?=<(rt|rp)([ \t\n\f\r]+[^ \x00-\x1F\x7F"'>/=]+([ \t\n\f\r]*=[ \t\n\f\r]*([^ \t\n\f\r"'=><`]+|'[^\x00-\x08\x0B\x0E-\x1F\x7F']*'|"[^\x00-\x08\x0B\x0E-\x1F\x7F"]*"))?)*[ \t\n\f\r]*>|</[0-9a-zA-Z]+[ \t\n\f\r]*>)

</rp>

An rp element's end tag may be omitted if the rp element is immediately followed by an rt or rp element, or if there is no more content in the parent element.

</rp> は直後に rt, rp 要素が続くか、親要素にそれ以上内容がなければ省略できる

簡易版
</rp\s*>(?=<(rt|rp)(\s+[^\s"'>/=]+(\s*=\s*([^\s"'=><`]+|'[^']*'|"[^"]*"))?)*\s*>|</[0-9a-zA-Z]+\s*>)
厳密版
</rp[ \t\n\f\r]*>(?=<(rt|rp)([ \t\n\f\r]+[^ \x00-\x1F\x7F"'>/=]+([ \t\n\f\r]*=[ \t\n\f\r]*([^ \t\n\f\r"'=><`]+|'[^\x00-\x08\x0B\x0E-\x1F\x7F']*'|"[^\x00-\x08\x0B\x0E-\x1F\x7F"]*"))?)*[ \t\n\f\r]*>|</[0-9a-zA-Z]+[ \t\n\f\r]*>)

</optgroup>

An optgroup element's end tag may be omitted if the optgroup element is immediately followed by another optgroup element, or if there is no more content in the parent element.

</optgroup> は直後に別の optgroup 要素が続くか、親要素にそれ以上内容がなければ省略できる

簡易版
</optgroup\s*>(?=<optgroup(\s+[^\s"'>/=]+(\s*=\s*([^\s"'=><`]+|'[^']*'|"[^"]*"))?)*\s*>|</[0-9a-zA-Z]+\s*>)
厳密版
</optgroup[ \t\n\f\r]*>(?=<optgroup([ \t\n\f\r]+[^ \x00-\x1F\x7F"'>/=]+([ \t\n\f\r]*=[ \t\n\f\r]*([^ \t\n\f\r"'=><`]+|'[^\x00-\x08\x0B\x0E-\x1F\x7F']*'|"[^\x00-\x08\x0B\x0E-\x1F\x7F"]*"))?)*[ \t\n\f\r]*>|</[0-9a-zA-Z]+[ \t\n\f\r]*>)

</option>

An option element's end tag may be omitted if the option element is immediately followed by another option element, or if it is immediately followed by an optgroup element, or if there is no more content in the parent element.

</option> は直後に option, optgroup 要素が続くか、親要素にそれ以上内容がなければ省略できる

簡易版
</option\s*>(?=<(option|optgroup)(\s+[^\s"'>/=]+(\s*=\s*([^\s"'=><`]+|'[^']*'|"[^"]*"))?)*\s*>|</[0-9a-zA-Z]+\s*>)
厳密版
</option[ \t\n\f\r]*>(?=<(option|optgroup)([ \t\n\f\r]+[^ \x00-\x1F\x7F"'>/=]+([ \t\n\f\r]*=[ \t\n\f\r]*([^ \t\n\f\r"'=><`]+|'[^\x00-\x08\x0B\x0E-\x1F\x7F']*'|"[^\x00-\x08\x0B\x0E-\x1F\x7F"]*"))?)*[ \t\n\f\r]*>|</[0-9a-zA-Z]+[ \t\n\f\r]*>)

<colgroup>

未完成

A colgroup element's start tag may be omitted if the first thing inside the colgroup element is a col element, and if the element is not immediately preceded by another colgroup element whose end tag has been omitted. (It can't be omitted if the element is empty.)

<colgroup> はその最初の内容が col 要素で、直前に終了タグを省略した colgroup 要素がなければ省略できる (ただし空要素の場合省略できない)

最初の内容がcol要素の`<colgroup>`(簡易版)
<colgroup\s*>(?=<col(\s+[^\s"'>/=]+(\s*=\s*([^\s"'=><`]+|'[^']*'|"[^"]*"))?)*\s*/?>)
最初の内容がcol要素の`<colgroup>`(厳密版)
<colgroup[ \t\n\f\r]*>(?=<col([ \t\n\f\r]+[^ \x00-\x1F\x7F"'>/=]+([ \t\n\f\r]*=[ \t\n\f\r]*([^ \t\n\f\r"'=><`]+|'[^\x00-\x08\x0B\x0E-\x1F\x7F']*'|"[^\x00-\x08\x0B\x0E-\x1F\x7F"]*"))?)*[ \t\n\f\r]*/?>)

Note

  1. 終了タグを省略したX要素にマッチしないパターンの戻り読みが出来ず、解決方法が不明。
    殆どの環境で可変長テキストの戻り読みはサポートされておらず、多分詰んでいる。

  2. (ただし空要素の場合省略できない)

    ここで言う空要素が何を示すのか不明。

    • 省略対象の colgroup 要素は、そもそも子要素に col 要素が含まれなければならず、空要素とはならない。
    • 内容に当たる col 要素は、そもそも常に空要素(閉じタグは無し)。
    • 直前の colgroup 要素については、空要素(中身の無い要素、閉じタグはルールにより省略不可)でも問題なし。
    (It can't be omitted if the element is empty.)

    原文にはこうある(「ただし」と書かれてはいない)ので、省略対象の colgroup 要素の事だと思われるが…

例示

※要素(タグ)の間にある改行や空白文字、説明用のHTMLコメントは無視するものとする。

<table>
  <caption>保元の乱</caption>
  <colgroup><!-- 直前にはcolgroup要素そのものが無いため、省略可 -->
    <col>
  </colgroup>
  <colgroup><!-- 直前のcolgroup要素は終了タグが省略されていないため、省略可 -->
    <col style="background:#f96;">
  <colgroup><!-- 直前のcolgroup要素は終了タグが省略されており、省略不可 -->
    <col style="background:#ffc;">
    <col style="background:#9fc;">
    <col style="background:#69f;">
  <tr>
    <th>
    <th>天皇家
    <th>藤原氏
    <th>平氏
    <th>源氏
  <tr>
    <th>天皇方
    <td>後白河
    <td>忠通
    <td>清盛
    <td>義朝
  <tr>
    <th>上皇方
    <td>崇徳
    <td>頼長
    <td>忠正
    <td>為義
</table>

参考: colgroup要素|(X)HTML辞典

</colgroup>

A colgroup element's end tag may be omitted if the colgroup element is not immediately followed by a space character or a comment.

</colgroup> は直後に空白文字かコメントがなければ省略できる

簡易版
</colgroup\s*>(?!\s|<!--[\s\S]*?-->)
厳密版
</colgroup[ \t\n\f\r]*>(?![ \t\n\f\r]|<!--[\s\S]*?-->)

</caption>

A caption element's end tag may be omitted if the caption element is not immediately followed by a space character or a comment.

</caption> は直後に空白文字かコメントがなければ省略できる

簡易版
</caption\s*>(?!\s|<!--[\s\S]*?-->)
厳密版
</caption[ \t\n\f\r]*>(?![ \t\n\f\r]|<!--[\s\S]*?-->)

</thead>

A thead element's end tag may be omitted if the thead element is immediately followed by a tbody or tfoot element.

</thead> は直後に tbody, tfoot 要素が続けば省略できる

簡易版
</thead\s*>(?=<(tbody|tfoot)(\s+[^\s"'>/=]+(\s*=\s*([^\s"'=><`]+|'[^']*'|"[^"]*"))?)*\s*>)
厳密版
</thead[ \t\n\f\r]*>(?=<(tbody|tfoot)([ \t\n\f\r]+[^ \x00-\x1F\x7F"'>/=]+([ \t\n\f\r]*=[ \t\n\f\r]*([^ \t\n\f\r"'=><`]+|'[^\x00-\x08\x0B\x0E-\x1F\x7F']*'|"[^\x00-\x08\x0B\x0E-\x1F\x7F"]*"))?)*[ \t\n\f\r]*>)

<tbody>

未完成

A tbody element's start tag may be omitted if the first thing inside the tbody element is a tr element, and if the element is not immediately preceded by a tbody, thead, or tfoot element whose end tag has been omitted. (It can't be omitted if the element is empty.)

<tbody> はその最初の内容が tr で、直前に終了タグを省略した tbody, thead, tfoot 要素がなければ省略できる

最初の内容がtr要素の`<tbody>`(簡易版)
<tbody\s*>(?=<tr(\s+[^\s"'>/=]+(\s*=\s*([^\s"'=><`]+|'[^']*'|"[^"]*"))?)*\s*>)
最初の内容がtr要素の`<tbody>`(厳密版)
<tbody[ \t\n\f\r]*>(?=<tr([ \t\n\f\r]+[^ \x00-\x1F\x7F"'>/=]+([ \t\n\f\r]*=[ \t\n\f\r]*([^ \t\n\f\r"'=><`]+|'[^\x00-\x08\x0B\x0E-\x1F\x7F']*'|"[^\x00-\x08\x0B\x0E-\x1F\x7F"]*"))?)*[ \t\n\f\r]*>)

Note

終了タグを省略したX要素にマッチしないパターンの戻り読みが出来ず、解決方法が不明。
殆どの環境で可変長テキストの戻り読みはサポートされておらず、多分詰んでいる。

</tbody>

A tbody element's end tag may be omitted if the tbody element is immediately followed by a tbody or tfoot element, or if there is no more content in the parent element.

</tbody> は直後に tbody, tfoot 要素が続くか、親要素にそれ以上内容がなければ省略できる

簡易版
</tbody\s*>(?=<(tbody|tfoot)(\s+[^\s"'>/=]+(\s*=\s*([^\s"'=><`]+|'[^']*'|"[^"]*"))?)*\s*>|</[0-9a-zA-Z]+\s*>)
厳密版
</tbody[ \t\n\f\r]*>(?=<(tbody|tfoot)([ \t\n\f\r]+[^ \x00-\x1F\x7F"'>/=]+([ \t\n\f\r]*=[ \t\n\f\r]*([^ \t\n\f\r"'=><`]+|'[^\x00-\x08\x0B\x0E-\x1F\x7F']*'|"[^\x00-\x08\x0B\x0E-\x1F\x7F"]*"))?)*[ \t\n\f\r]*>|</[0-9a-zA-Z]+[ \t\n\f\r]*>)

</tfoot>

A tfoot element's end tag may be omitted if the tfoot element is immediately followed by a tbody element, or if there is no more content in the parent element.

</tfoot> は直後に tbody 要素が続くか、親要素にそれ以上内容がなければ省略できる

簡易版
</tfoot\s*>(?=<tbody(\s+[^\s"'>/=]+(\s*=\s*([^\s"'=><`]+|'[^']*'|"[^"]*"))?)*\s*>|</[0-9a-zA-Z]+\s*>)
厳密版
</tfoot[ \t\n\f\r]*>(?=<tbody([ \t\n\f\r]+[^ \x00-\x1F\x7F"'>/=]+([ \t\n\f\r]*=[ \t\n\f\r]*([^ \t\n\f\r"'=><`]+|'[^\x00-\x08\x0B\x0E-\x1F\x7F']*'|"[^\x00-\x08\x0B\x0E-\x1F\x7F"]*"))?)*[ \t\n\f\r]*>|</[0-9a-zA-Z]+[ \t\n\f\r]*>)

</tr>

A tr element's end tag may be omitted if the tr element is immediately followed by another tr element, or if there is no more content in the parent element.

</tr> は直後に tr 要素が続くか、親要素にそれ以上内容がなければ省略できる

簡易版
</tr\s*>(?=<tr(\s+[^\s"'>/=]+(\s*=\s*([^\s"'=><`]+|'[^']*'|"[^"]*"))?)*\s*>|</[0-9a-zA-Z]+\s*>)
厳密版
</tr[ \t\n\f\r]*>(?=<tr([ \t\n\f\r]+[^ \x00-\x1F\x7F"'>/=]+([ \t\n\f\r]*=[ \t\n\f\r]*([^ \t\n\f\r"'=><`]+|'[^\x00-\x08\x0B\x0E-\x1F\x7F']*'|"[^\x00-\x08\x0B\x0E-\x1F\x7F"]*"))?)*[ \t\n\f\r]*>|</[0-9a-zA-Z]+[ \t\n\f\r]*>)

</td>

A td element's end tag may be omitted if the td element is immediately followed by a td or th element, or if there is no more content in the parent element.

</td> は直後に td, th 要素が続くか、親要素にそれ以上内容がなければ省略できる

簡易版
</td\s*>(?=<(td|th)(\s+[^\s"'>/=]+(\s*=\s*([^\s"'=><`]+|'[^']*'|"[^"]*"))?)*\s*>|</[0-9a-zA-Z]+\s*>)
厳密版
</td[ \t\n\f\r]*>(?=<(td|th)([ \t\n\f\r]+[^ \x00-\x1F\x7F"'>/=]+([ \t\n\f\r]*=[ \t\n\f\r]*([^ \t\n\f\r"'=><`]+|'[^\x00-\x08\x0B\x0E-\x1F\x7F']*'|"[^\x00-\x08\x0B\x0E-\x1F\x7F"]*"))?)*[ \t\n\f\r]*>|</[0-9a-zA-Z]+[ \t\n\f\r]*>)

</th>

A th element's end tag may be omitted if the th element is immediately followed by a td or th element, or if there is no more content in the parent element.

</th> は直後に td, th 要素が続くか、親要素にそれ以上内容がなければ省略できる

簡易版
</th\s*>(?=<(td|th)(\s+[^\s"'>/=]+(\s*=\s*([^\s"'=><`]+|'[^']*'|"[^"]*"))?)*\s*>|</[0-9a-zA-Z]+\s*>)
厳密版
</th[ \t\n\f\r]*>(?=<(td|th)([ \t\n\f\r]+[^ \x00-\x1F\x7F"'>/=]+([ \t\n\f\r]*=[ \t\n\f\r]*([^ \t\n\f\r"'=><`]+|'[^\x00-\x08\x0B\x0E-\x1F\x7F']*'|"[^\x00-\x08\x0B\x0E-\x1F\x7F"]*"))?)*[ \t\n\f\r]*>|</[0-9a-zA-Z]+[ \t\n\f\r]*>)

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
24