开发者

RegEx get tr tags

I have the string:

'
<tr id="sdf"><开发者_StackOverflow;/tr>
<td>
  <div>asdf</div>
  asdf
</td>
<tr id="sdfdf">
  <td>
    <div>asdf</div>
    asdf
  </td>
</tr>
<tr id="sdf"></tr>
<tr id="ssdfdf">
  <td>
    <div>asdf</div>
    asdf
  </td>
</tr>
'

and I'd like to save tr tags into an array using RegExp.


As long as <tr> tags are never nested, you could try this:

result = subject.match(/<tr[\s\S]*?<\/tr>/g);

This gets you an array of all <tr> tags and their contents.

[\s\S] is the JavaScript way of saying "any character, including newlines", and *? asks for zero or more repetitions of that, trying to use as few as possible to avoid matching across multiple tags at once.

This blows up as soon as <tr> tags are nested, though, which is one of the reasons why regexes are not the best tool for parsing markup languages (to put it mildly). You will get more reliable results by parsing the DOM.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜