admin管理员组

文章数量:1425717

I have the string:

'
<tr id="sdf"></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.

I have the string:

'
<tr id="sdf"></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.

Share Improve this question edited Feb 23, 2011 at 11:55 Bart Kiers 170k37 gold badges306 silver badges295 bronze badges asked Feb 23, 2011 at 11:05 AlexanderAlexander 6073 gold badges11 silver badges23 bronze badges 12
  • 3 On what? An elephant? A wart? Is this an XHTML question? What is the source data? What do you want to do with the result? Also, parsing XHTML with Regex is almost always wrong. – Lightness Races in Orbit Commented Feb 23, 2011 at 11:06
  • By using a HTML parser... you have to provide a lot more information. – Felix Kling Commented Feb 23, 2011 at 11:06
  • 1 @Alexander: Do you practise being vague? – Lightness Races in Orbit Commented Feb 23, 2011 at 11:10
  • 2 Be gentle Tomalak, he doesn't sound like a native English speaker. But Alexader, we need more information. Are you looking for content within the tr tags, are you looking for the entire element with the content? Tell us what exactly you're trying to do. Chances are, someone will tell you a better way to get it done. – HyderA Commented Feb 23, 2011 at 11:12
  • 1 When I use "<tr.+</tr>" it return me incorrect answer. It does not take into account that tr tag cannot contain another tr tags. – Alexander Commented Feb 23, 2011 at 11:13
 |  Show 7 more ments

1 Answer 1

Reset to default 5

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.

本文标签: javascriptRegEx get tr tagsStack Overflow