代码之家  ›  专栏  ›  技术社区  ›  bugrasitemkar

用javascript格式化文本,其中包含带有正则表达式的重复文本块

  •  -1
  • bugrasitemkar  · 技术社区  · 7 年前

    我有一个包含相同结构文本的重复块的文本,如下所示:

    From marvin.seegers@gmail.com Fri Aug 11 10:35:14 2017 Return-Path:
    <crowdwiggel@n1plcpnl0114.prod.ams1.secureserver.net>
      Delivered-To: buchung@think-square.de Received: from n1plcpnl0114.prod.ams1.secureserver.net by n1plcpnl0114.prod.ams1.secureserver.net (Dovecot) with LMTP id CdUzIUJsjVleYAQAtX1LSA for
      <buchung@think-square.de>; Fri, 11 Aug 2017 01:35:14 -0700 Return-path:
        <crowdwiggel@n1plcpnl0114.prod.ams1.secureserver.net>
          Envelope-to: buchung@think-square.de Delivery-date: Fri, 11 Aug 2017 01:35:14 -0700 Received: from crowdwiggel by n1plcpnl0114.prod.ams1.secureserver.net with local (Exim 4.88) (envelope-from
          <crowdwiggel@n1plcpnl0114.prod.ams1.secureserver.net>) id 1dg5PW-001Eda-Ep for buchung@think-square.de; Fri, 11 Aug 2017 01:35:14 -0700 To: buchung@think-square.de Subject: You received a booking request. X-PHP-Script: www.think-square.de/wp-admin/admin-ajax.php for 79.230.85.192 Date: Fri, 11 Aug
            2017 08:35:14 +0000 From: "marvin.seegers@gmail.com"
            <marvin.seegers@gmail.com>
              Reply-To: "marvin.seegers@gmail.com"
              <marvin.seegers@gmail.com>
                Message-ID:
                <e65b7055255651bc19d658f155d8bc7c@www.think-square.de>
                  X-Mailer: PHPMailer 5.2.22 (https://github.com/PHPMailer/PHPMailer) MIME-Version: 1.1 MIME-Version: 1.0 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable Below are the details. Go to admin to cancel the request.=0A<br
                  /><br />=0A=
                  <h3>Details</h3>
                  <table>
                    <tbody>
                      <tr>
                        <td style=3D "vertical-align: to=
    p; width: 150px;"><strong style=3D "color: #898989;">Reservierungsnummer</st=
    rong></td>     <td style=3D"vertical-align: top;"><span style=3D"color: #66=
    6666;">1952</em></td></tr><tr>     <td style=3D"vertical-align: top; width:=
     150px;"><strong style=3D"color: #898989;">Kalender</strong></td>
                        <td s=t yle=3D "vertical-align: top;"><span style=3D "color: #666666;">7</em></td></=
    tr><tr>     <td style=3D"vertical-align: top; width: 150px;"><strong style=
    =3D"color: #898989;">Escape Room</strong></td>     <td style=3D"vertical-al=
    ign: top;"><span style=3D"color: #666666;">Triaden</em></td></tr><tr>     <=
    td style=3D"vertical-align: top; width: 150px;"><strong style=3D"color: #89=
    8989;">Gew=C3=A4hlte Sprache</strong></td>     <td style=3D"vertical-align:=
     top;"><span style=3D"color: #666666;">German (Deutsch)</em></td></tr>     =
    </tbody></table><br /><table>     <tbody><tr>     <td style=3D"vertical-ali=
    gn: top; width: 150px;"><strong style=3D"color: #898989;">Anreise</strong><=
    /td>     <td style=3D"vertical-align: top;"><span style=3D"color: #666666;"=
    >23 September 2017</em></td></tr><tr>     <td style=3D"vertical-align: top;=
     width: 150px;"><strong style=3D"color: #898989;">Start um</strong></td>   =
      <td style=3D"vertical-align: top;"><span style=3D"color: #666666;">20:30<=
    /em></td></tr>     </tbody></table><br /><table>     <tbody><tr>     <td st=
    yle=3D"vertical-align: top; width: 150px;"><strong style=3D"color: #898989;=
    ">Zahlungsmethode</strong></td>     <td style=3D"vertical-align: top;"><spa=
    n style=3D"color: #666666;">On arrival</em></td></tr><tr>     <td style=3D"=
    vertical-align: top; width: 150px;"><strong style=3D"color: #252525;">Summe=
    </strong></td>     <td style=3D"vertical-align: top;"><strong style=3D"colo=
    r: #ff6300;">110&#8364;</strong></td></tr>     </tbody></table>=0A<br /><br=
     />=0A<h3>Auswahl</h3><table>     <tbody><tr>     <td style=3D"vertical-ali=
    gn: top; width: 150px;"><strong style=3D"color: #898989;">Gruppengr=C3=
    =B6=C3=9Fe</strong></td>     <td style=3D"vertical-align: top;"><span style=
    =3D"color: #666666;">5 Personen<br /><strong>+&nbsp;110&#8364;</strong></em=
    ></td></tr><br /><tr>     <td style=3D"vertical-align: top; width: 150px;">=
    <strong style=3D"color: #252525;">Gesamtbetrag</strong></td>     <td style=
    =3D"vertical-align: top;"><strong style=3D"color: #252525;">+&nbsp;110&#836=
    4;</strong></td></tr>     </tbody></table>=0A<br /><br />=0A<h3>Discount</h=
    3><em>No discount.</em>=0A<br /><br />=0A<h3>Coupons</h3><em>No coupon.</em=
    >=0A<br /><br />=0A<h3>Taxes & fees</h3><em>No taxes or fees.</em>=0A<br />=
    <br />=0A<h3><center><b>Kontaktinformationen</b></center></h3><table>     <=
    tbody><tr>     <td style=3D"vertical-align: top; width: 150px;"><strong sty=
    le=3D"color: #898989;">Vorname</strong></td>     <td style=3D"vertical-alig=
    n: top;"><span style=3D"color: #666666;">Marvin</em></td></tr><tr>     <td =
    style=3D"vertical-align: top; width: 150px;"><strong style=3D"color: #89898=
    9;">Nachname</strong></td>     <td style=3D"vertical-align: top;"><span sty=
    le=3D"color: #666666;">Seegers</em></td></tr><tr>     <td style=3D"vertical=
    -align: top; width: 150px;"><strong style=3D"color: #898989;">Email</strong=
    ></td>     <td style=3D"vertical-align: top;"><span style=3D"color: #666666=
    ;"><a href=3D"mailto:marvin.seegers@gmail.com">marvin.seegers@gmail.com</a>=
    </em></td></tr><tr>     <td style=3D"vertical-align: top; width: 150px;"><s=
    trong style=3D"color: #898989;">Telefon</strong></td>     <td style=3D"vert=
    ical-align: top;"><span style=3D"color: #666666;">01726327217</em></td></tr=
    ><tr>     <td style=3D"vertical-align: top; width: 150px;"><strong style=3D=
    "color: #898989;">Gutscheincode (bitte Zahlung vor Ort ausw=C3=A4hlen)</str=
    ong></td>     <td style=3D"vertical-align: top;"><em style=3D"color: #acaca=
    c;">Form field was not completed.</em></td></tr><tr>     <td style=3D"verti=
    cal-align: top; width: 150px;"><strong style=3D"color: #898989;">Nachricht<=
    /strong></td>     <td style=3D"vertical-align: top;"><span style=3D"color: =
    #666666;">Hey!=0AIch w=C3=BCrde den Abend gerne meinen Geburtstag bei euch =
    nachfeiern.=0A=0AGr=C3=BC=C3=9Fe</em></td></tr><tr>     <td style=3D"vertic=
    al-align: top; width: 150px;"><strong style=3D"color: #898989;">Wie habe ic=
    h von Think Square erfahren</strong></td>     <td style=3D"vertical-align: =
    top;"><span style=3D"color: #666666;">Google</em></td></tr>     </tbody></t=
    able>=0A<br /><br />=0A<h3>Billing address</h3><table>     <tbody><em>No bi=
    lling address.</em>     </tbody></table>=0A<br /><br />=0A<h3>Shipping addr=
    ess</h3><table>     <tbody><em>No shipping address.</em>     </tbody></tabl=
    e>
    
    From katharina.lensing@hotmail.de Fri Aug 11 07:36:47 2017
    Return-Path: <crowdwiggel@n1plcpnl0114.prod.ams1.secureserver.net>
    Delivered-To: buchung@think-square.de
    Received: from n1plcpnl0114.prod.ams1.secureserver.net
    	by n1plcpnl0114.prod.ams1.secureserver.net (Dovecot) with LMTP id iVh0Cm9CjVmsaAAAtX1LSA
    	for <buchung@think-square.de>; Thu, 10 Aug 2017 22:36:47 -0700
    Return-path: <crowdwiggel@n1plcpnl0114.prod.ams1.secureserver.net>
    Envelope-to: buchung@think-square.de
    Delivery-date: Thu, 10 Aug 2017 22:36:47 -0700
    Received: from crowdwiggel by n1plcpnl0114.prod.ams1.secureserver.net with local (Exim 4.88)
    	(envelope-from <crowdwiggel@n1plcpnl0114.prod.ams1.secureserver.net>)
    	id 1dg2cp-00082R-2s
    	for buchung@think-square.de; Thu, 10 Aug 2017 22:36:47 -0700
    To: buchung@think-square.de
    Subject: You received a booking request.
    X-PHP-Script: www.think-square.de/wp-admin/admin-ajax.php for 91.56.142.96
    Date: Fri, 11 Aug 2017 05:36:46 +0000
    From: "katharina.lensing@hotmail.de" <katharina.lensing@hotmail.de>
    Reply-To: "katharina.lensing@hotmail.de" <katharina.lensing@hotmail.de>
    Message-ID: <a6cbfd346364029b8055f58d18ac4d89@www.think-square.de>
    X-Mailer: PHPMailer 5.2.22 (https://github.com/PHPMailer/PHPMailer)
    MIME-Version: 1.1
    MIME-Version: 1.0
    Content-Type: text/html; charset=utf-8
    Content-Transfer-Encoding: quoted-printable
    
    Below are the details. Go to admin to cancel the request.=0A<br /><br />=0A=
    <h3>Details</h3><table>     <tbody><tr>     <td style=3D"vertical-align: to=
    p; width: 150px;"><strong style=3D"color: #898989;">Reservierungsnummer</st=
    rong></td>     <td style=3D"vertical-align: top;"><span style=3D"color: #66=
    6666;">1951</em></td></tr><tr>     <td style=3D"vertical-align: top; width:=
     150px;"><strong style=3D"color: #898989;">Kalender</strong></td>     <td s=
    tyle=3D"vertical-align: top;"><span style=3D"color: #666666;">4</em></td></=
    tr><tr>     <td style=3D"vertical-align: top; width: 150px;"><strong style=
    =3D"color: #898989;">Escape Room</strong></td>     <td style=3D"vertical-al=
    ign: top;"><span style=3D"color: #666666;">Contagious</em></td></tr><tr>   =
      <td style=3D"vertical-align: top; width: 150px;"><strong style=3D"color: =
    #898989;">Gew=C3=A4hlte Sprache</strong></td>     <td style=3D"vertical-ali=
    gn: top;"><span style=3D"color: #666666;">German (Deutsch)</em></td></tr>  =
       </tbody></table><br /><table>     <tbody><tr>     <td style=3D"vertical-=
    align: top; width: 150px;"><strong style=3D"color: #898989;">Anreise</stron=
    g></td>     <td style=3D"vertical-align: top;"><span style=3D"color: #66666=
    6;">11 August 2017</em></td></tr><tr>     <td style=3D"vertical-align: top;=
     width: 150px;"><strong style=3D"color: #898989;">Start um</strong></td>   =
      <td style=3D"vertical-align: top;"><span style=3D"color: #666666;">19:00<=
    /em></td></tr>     </tbody></table><br /><table>     <tbody><tr>     <td st=
    yle=3D"vertical-align: top; width: 150px;"><strong style=3D"color: #898989;=
    ">Zahlungsmethode</strong></td>     <td style=3D"vertical-align: top;"><spa=
    n style=3D"color: #666666;">On arrival</em></td></tr><tr>     <td style=3D"=
    vertical-align: top; width: 150px;"><strong style=3D"color: #252525;">Summe=
    </strong></td>     <td style=3D"vertical-align: top;"><strong style=3D"colo=
    r: #ff6300;">110&#8364;</strong></td></tr>     </tbody></table>=0A<br /><br=
     />=0A<h3>Auswahl</h3><table>     <tbody><tr>     <td style=3D"vertical-ali=
    gn: top; width: 150px;"><strong style=3D"color: #898989;">Gruppengr=C3=
    =B6=C3=9Fe</strong></td>     <td style=3D"vertical-align: top;"><span style=
    =3D"color: #666666;">5 Personen<br /><strong>+&nbsp;110&#8364;</strong></em=
    ></td></tr><br /><tr>     <td style=3D"vertical-align: top; width: 150px;">=
    <strong style=3D"color: #252525;">Gesamtbetrag</strong></td>     <td style=
    =3D"vertical-align: top;"><strong style=3D"color: #252525;">+&nbsp;110&#836=
    4;</strong></td></tr>     </tbody></table>=0A<br /><br />=0A<h3>Discount</h=
    3><em>No discount.</em>=0A<br /><br />=0A<h3>Coupons</h3><em>No coupon.</em=
    >=0A<br /><br />=0A<h3>Taxes & fees</h3><em>No taxes or fees.</em>=0A<br />=
    <br />=0A<h3><center><b>Kontaktinformationen</b></center></h3><table>     <=
    tbody><tr>     <td style=3D"vertical-align: top; width: 150px;"><strong sty=
    le=3D"color: #898989;">Vorname</strong></td>     <td style=3D"vertical-alig=
    n: top;"><span style=3D"color: #666666;">Katharina</em></td></tr><tr>     <=
    td style=3D"vertical-align: top; width: 150px;"><strong style=3D"color: #89=
    8989;">Nachname</strong></td>     <td style=3D"vertical-align: top;"><span =
    style=3D"color: #666666;">Lensing</em></td></tr><tr>     <td style=3D"verti=
    cal-align: top; width: 150px;"><strong style=3D"color: #898989;">Email</str=
    ong></td>     <td style=3D"vertical-align: top;"><span style=3D"color: #666=
    666;"><a href=3D"mailto:katharina.lensing@hotmail.de">katharina.lensing@hot=
    mail.de</a></em></td></tr><tr>     <td style=3D"vertical-align: top; width:=
     150px;"><strong style=3D"color: #898989;">Telefon</strong></td>     <td st=
    yle=3D"vertical-align: top;"><span style=3D"color: #666666;">01637908883</e=
    m></td></tr><tr>     <td style=3D"vertical-align: top; width: 150px;"><stro=
    ng style=3D"color: #898989;">Gutscheincode (bitte Zahlung vor Ort ausw=
    =C3=A4hlen)</strong></td>     <td style=3D"vertical-align: top;"><em style=
    =3D"color: #acacac;">Form field was not completed.</em></td></tr><tr>     <=
    td style=3D"vertical-align: top; width: 150px;"><strong style=3D"color: #89=
    8989;">Nachricht</strong></td>     <td style=3D"vertical-align: top;"><em s=
    tyle=3D"color: #acacac;">Form field was not completed.</em></td></tr><tr>  =
       <td style=3D"vertical-align: top; width: 150px;"><strong style=3D"color:=
     #898989;">Wie habe ich von Think Square erfahren</strong></td>     <td sty=
    le=3D"vertical-align: top;"><span style=3D"color: #666666;">Google</em></td=
    ></tr>     </tbody></table>=0A<br /><br />=0A<h3>Billing address</h3><table=
    >     <tbody><em>No billing address.</em>     </tbody></table>=0A<br /><br =
    />=0A<h3>Shipping address</h3><table>     <tbody><em>No shipping address.</=
    em>     </tbody></table>

    我愿意把一段文字从“from”开始,在“other from”开始之前结束。 我已经试过了

    var subStr = srctext.match("From (.*) From");
    
    var re = /(.*From\s+)(.*)(\s+From.*)/;
    var newtext = srctext.replace(re, "$2");
    

    和上面正则表达式的一些变体,但还没有运气

    2 回复  |  直到 7 年前
        1
  •  1
  •   pchaigno    7 年前

    String.prototype.split 相反:

    var newtext = srctext.split(/(?:\n|^)From\s+/);
    

    newtext[1] newtext[2]

    (?:\n|^)From 从…起

    here

        2
  •  0
  •   leoinstack    7 年前

    From ([\s\S]*) From