要求:
A:提取所有的<math>标记和内容(math有的包含属性有的没有)
B:<math>中不能出现<math>
C:ie和ff都要能通过
在线验证地址:
http://thinkbase.net/w/main/Wiki/Javascript+regex+test+page.html?action=action_view_attachment&attachment=Javascript+%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F%E6%B5%8B%E8%AF%95%E9%A1%B5%E9%9D%A2.Javascript+regex+test+page.html我些的正则式:<math(.*)>(.*)</math>但是匹配不出内容
验证内容:
<?xml version="1.0"?>
<!-- Copy of ASCIIMathML input
For example `int_-1^1 sqrt(1-x^2)dx = pi/2`,`((a,b),(c,d))^-1 = 1/(ad-bc)((d,-b),(-c,a))`and $\sinh x=\frac{e^x-e^{-x}}{2}$.amath
Or try ASCIIMath without delimiters: like x^2 or sin xIt can be quite convenient...
endamath-->
<?xml-stylesheet type="text/xsl" href="http://www1.chapman.edu/~jipsen/mathml/pmathml.xsl"?>
<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:mml="http://www.w3.org/1998/Math/MathML">
<head>
<title>...</title>
</head>
<body>
For example 
  <span style="font-size: 1em; font-family: serif;">
    <math title="int_-1^1 sqrt(1-x^2)dx = pi/2" xmlns="http://www.w3.org/1998/Math/MathML">
      <mstyle mathcolor="blue" fontfamily="serif" displaystyle="true">
        <mrow>
          <msubsup>
            <mo>&#8747;</mo>
            <mrow>
              <mo>-</mo>
              <mn>1</mn>
            </mrow>
            <mn>1</mn>
          </msubsup>
        </mrow>
        <msqrt>
          <mrow>
            <mn>1</mn>
            <mo>-</mo>
            <msup>
              <mi>x</mi>
              <mn>2</mn>
            </msup>
          </mrow>
        </msqrt>
        <mrow>
          <mi>d</mi>
          <mi>x</mi>
        </mrow>
        <mo>=</mo>
        <mfrac>
          <mi>&#960;</mi>
          <mn>2</mn>
        </mfrac>
      </mstyle>
    </math>
  </span>,
  <p>
  </p>
  <span style="font-size: 1em; font-family: serif;">
    <math title="((a,b),(c,d))^-1 = 1/(ad-bc)((d,-b),(-c,a))" xmlns="http://www.w3.org/1998/Math/MathML">
      <mstyle mathcolor="blue" fontfamily="serif" displaystyle="true">
        <msup>
          <mrow>
            <mo>(</mo>
            <mtable>
              <mtr>
                <mtd>
                  <mi>a</mi>
                </mtd>
                <mtd>
                  <mi>b</mi>
                </mtd>
              </mtr>
              <mtr>
                <mtd>
                  <mi>c</mi>
                </mtd>
                <mtd>
                  <mi>d</mi>
                </mtd>
              </mtr>
            </mtable>
            <mo>)</mo>
          </mrow>
          <mrow>
            <mo>-</mo>
            <mn>1</mn>
          </mrow>
        </msup>
        <mo>=</mo>
        <mfrac>
          <mn>1</mn>
          <mrow>
            <mi>a</mi>
            <mi>d</mi>
            <mo>-</mo>
            <mi>b</mi>
            <mi>c</mi>
          </mrow>
        </mfrac>
        <mrow>
          <mo>(</mo>
          <mtable>
            <mtr>
              <mtd>
                <mi>d</mi>
              </mtd>
              <mtd>
                <mo>-</mo>
                <mi>b</mi>
              </mtd>
            </mtr>
            <mtr>
              <mtd>
                <mo>-</mo>
                <mi>c</mi>
              </mtd>
              <mtd>
                <mi>a</mi>
              </mtd>
            </mtr>
          </mtable>
          <mo>)</mo>
        </mrow>
      </mstyle>
    </math>
  </span>
  <p>
  </p>and 
  <span style="font-size: 1em; font-family: serif;">
    <math title="\sinh x=\frac{e^x-e^{-x}}{2}" xmlns="http://www.w3.org/1998/Math/MathML">
      <mstyle fontfamily="serif">
        <mrow>
          <mi -moz-math-font-style="normal">sinh</mi>
          <mi>x</mi>
        </mrow>
        <mo>=</mo>
        <mfrac>
          <mrow>
            <mrow>
              <msup>
                <mi>e</mi>
                <mi>x</mi>
              </msup>
            </mrow>
            <mo>-</mo>
            <mrow>
              <msup>
                <mi>e</mi>
                <mrow>
                  <mo>-</mo>
                  <mi>x</mi>
                </mrow>
              </msup>
            </mrow>
          </mrow>
          <mrow>
            <mn>2</mn>
          </mrow>
        </mfrac>
      </mstyle>
    </math>
  </span>.
  <p>
  </p>&#10;Or try ASCIIMath without delimiters: like 
  <span style="font-size: 1em; font-family: serif;">
    <math title="x^2" xmlns="http://www.w3.org/1998/Math/MathML">
      <mstyle mathcolor="blue" fontfamily="serif" displaystyle="true">
        <msup>
          <mi>x</mi>
          <mn>2</mn>
        </msup>
      </mstyle>
    </math>
  </span> or 
  <span style="font-size: 1em; font-family: serif;">
    <math title="sin x" xmlns="http://www.w3.org/1998/Math/MathML">
      <mstyle mathcolor="blue" fontfamily="serif" displaystyle="true">
        <mrow>
          <mo>sin</mo>
          <mi>x</mi>
        </mrow>
      </mstyle>
    </math>
  </span>
  <p>
  </p>It can be quite convenient...&#10;
  <p>
  </p>
  <span style="font-size: 1em; font-family: serif;">
    <math xmlns="http://www.w3.org/1998/Math/MathML">
      <mstyle mathcolor="blue" fontfamily="serif" displaystyle="true">
        <mo></mo>
      </mstyle>
    </math>
  </span>
</body>
</html>

解决方案 »

  1.   

    <script>
    str = "<math>aaaaa</math><math>bbbbb</math>";
    alert(str.match(/\<math.*?<\/math>/gi));
    </script>
      

  2.   

    <script>
    str = '<math title="((a,b),(c,d))^-1 = 1/(ad-bc)((d,-b),(-c,a))" xmlns="http://www.w3.org/1998/Math/MathML"><mstyle mathcolor="blue" fontfamily="serif" displaystyle="true"><msup>............</math><span style="font-size: 1em; font-family: serif;"><math><sdfsdf></sdfads></math></span>.......';
    alert(str.match(/\<math.*?<\/math>/gi));
    </script>
    你自己看吧. 结果将是以,分隔的数组.
      

  3.   

    比如你把最后的</math>换行就取不到了
      

  4.   

    /<math(.*?)>(.|\n|\r\n|\r)*?</math>/igm 这个才是正确的,不过还是谢谢你
      

  5.   

    <math(>|\s[^>]*>).*?</math>