Split a string of HTML into an array by particular tags

后端 未结 4 743
遇见更好的自我
遇见更好的自我 2021-01-05 00:41

Given this HTML as a string \"html\", how can I split it into an array where each header marks the start of an element?

Begin with this:

4条回答
  •  心在旅途
    2021-01-05 01:00

    In your example you can use:

    /
          // Match literal <
      .*?  // Match any character zero or more times, non greedy
      <\/h // Match literal     // Match literal >
    /g
    
    var str = '

    A

    B

    Foobar

    C

    ' str.match(/.*?<\/h\1>/g); // ["

    A

    ", "

    B

    ", "

    C

    "]

    But please don't parse HTML with regexp, read RegEx match open tags except XHTML self-contained tags

提交回复
热议问题