从<span>标记

时间:2017-08-02 05:59:52

标签: jquery regex

我有一个满意的<pre id="notes">,里面有一些<p>,还有一个名为#coffee的按钮。

当用户按下按钮#coffee时,它将扫描<p>内的<pre id="notes">以匹配正则表达式,如果找到,请打包匹配正则表达式的<span>

目前我只在<span>前面的<p>中添加匹配的文本和值,但是我想要包装匹配的正则表达式字。那么任何想法如何实现这种效果?这是我的代码:

  $("#coffee").on("click", function() {
     $("#notes > p, #notes * p").each(function() {
        var autoICD9 = $(this).text().match(/V\d{2}\.\d{1,2}|\d{3}\.\d{1,2}|E\d{3}\.\d|\b\d{3}\b/g);
         if(autoICD9 != null){ 
          //Want to make this wrap on matched text inside <p> instead
          $(this).prepend('<span class="ICD9x">'+autoICD9+'</span>');
        }  
     });
  });

这是<pre>

<pre class="ev-notes" id="notes" contenteditable="true"><p></p><p class="MsoNormal"><span style="font-family:&quot;PMingLiU&quot;,serif;mso-ascii-font-family:
Calibri;mso-ascii-theme-font:minor-latin;mso-fareast-font-family:PMingLiU;
mso-fareast-theme-font:minor-fareast;mso-hansi-font-family:Calibri;mso-hansi-theme-font:
minor-latin" lang="ZH-TW"><br></span></p><p></p><p class="MsoNormal">While there are no 401 in the Comparison group: did not have 401—405 throughout the
dataset.<br>
<span style="font-family:&quot;PMingLiU&quot;,serif;mso-bidi-font-family:PMingLiU;color:#222222"><span lang="ZH-TW"></span></span></p><p></p><p class="MsoNormal">&nbsp;</p><p></p><table class="MsoTableGrid" style="border-collapse:collapse;border:none;mso-border-alt:solid windowtext .5pt;
 mso-yfti-tbllook:1184;mso-padding-alt:0in 5.4pt 0in 5.4pt" cellspacing="0" cellpadding="0" border="1">
 <tbody><tr style="mso-yfti-irow:0;mso-yfti-firstrow:yes">
  <td style="width:131.25pt;border:solid windowtext 1.0pt;
  mso-border-alt:solid windowtext .5pt;padding:0in 5.4pt 0in 5.4pt" width="175" valign="top">
  <p class="MsoNormal" style="margin-bottom:0in;margin-bottom:.0001pt;line-height:
  normal">&nbsp;</p>
  </td>
  <td style="width:155.6pt;border:solid windowtext 1.0pt;
  border-left:none;mso-border-left-alt:solid windowtext .5pt;mso-border-alt:
  solid windowtext .5pt;padding:0in 5.4pt 0in 5.4pt" width="207" valign="top">
  <p class="MsoNormal" style="margin-bottom:0in;margin-bottom:.0001pt;line-height:
  normal">&nbsp;</p>
  </td>
  <td style="width:144.65pt;border:solid windowtext 1.0pt;
  border-left:none;mso-border-left-alt:solid windowtext .5pt;mso-border-alt:
  solid windowtext .5pt;padding:0in 5.4pt 0in 5.4pt" width="193" valign="top">
  <p class="MsoNormal" style="margin-bottom:0in;margin-bottom:.0001pt;line-height:
  normal">ICD-9-CM code(s)</p>
  </td>
 </tr>
 <tr style="mso-yfti-irow:1">
  <td style="width:131.25pt;border:solid windowtext 1.0pt;
  border-top:none;mso-border-top-alt:solid windowtext .5pt;mso-border-alt:solid windowtext .5pt;
  padding:0in 5.4pt 0in 5.4pt" width="175" valign="top">
  <p class="MsoNormal" style="margin-bottom:0in;margin-bottom:.0001pt;line-height:
  normal">Exposure</p>
  </td>
  <td style="width:155.6pt;border-top:none;border-left:
  none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;
  mso-border-top-alt:solid windowtext .5pt;mso-border-left-alt:solid windowtext .5pt;
  mso-border-alt:solid windowtext .5pt;padding:0in 5.4pt 0in 5.4pt" width="207" valign="top">
  <p class="MsoNormal" style="margin-bottom:0in;margin-bottom:.0001pt;line-height:
  normal">Essential hypertension</p>
  <p class="MsoNormal" style="margin-bottom:0in;margin-bottom:.0001pt;line-height:
  normal"><span style="font-family:&quot;PMingLiU&quot;,serif;mso-ascii-font-family:
  Calibri;mso-ascii-theme-font:minor-latin;mso-fareast-font-family:PMingLiU;
  mso-fareast-theme-font:minor-fareast;mso-hansi-font-family:Calibri;
  mso-hansi-theme-font:minor-latin" lang="ZH-TW"><br></span></p>
  </td>
  <td style="width:144.65pt;border-top:none;border-left:
  none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;
  mso-border-top-alt:solid windowtext .5pt;mso-border-left-alt:solid windowtext .5pt;
  mso-border-alt:solid windowtext .5pt;padding:0in 5.4pt 0in 5.4pt" width="193" valign="top">
  <p class="MsoNormal" style="margin-bottom:0in;margin-bottom:.0001pt;line-height:
  normal">401—405</p>
  </td>
 </tr>
 <tr style="mso-yfti-irow:2;mso-yfti-lastrow:yes">
  <td style="width:131.25pt;border:solid windowtext 1.0pt;
  border-top:none;mso-border-top-alt:solid windowtext .5pt;mso-border-alt:solid windowtext .5pt;
  padding:0in 5.4pt 0in 5.4pt" width="175" valign="top">
  <p class="MsoNormal" style="margin-bottom:0in;margin-bottom:.0001pt;line-height:
  normal">Outcome</p>
  </td>
  <td style="width:155.6pt;border-top:none;border-left:
  none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;
  mso-border-top-alt:solid windowtext .5pt;mso-border-left-alt:solid windowtext .5pt;
  mso-border-alt:solid windowtext .5pt;padding:0in 5.4pt 0in 5.4pt" width="207" valign="top">
  <p class="MsoNormal" style="margin-bottom:0in;margin-bottom:.0001pt;line-height:
  normal">Urinary bladder cancer + CIS</p>
  
  </td>
  <td style="width:144.65pt;border-top:none;border-left:
  none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;
  mso-border-top-alt:solid windowtext .5pt;mso-border-left-alt:solid windowtext .5pt;
  mso-border-alt:solid windowtext .5pt;padding:0in 5.4pt 0in 5.4pt" width="193" valign="top">
  <p class="MsoNormal" style="margin-bottom:0in;margin-bottom:.0001pt;line-height:
  normal">188, 233.7</p>
  </td>
 </tr>
</tbody></table><p></p><p class="MsoNormal">&nbsp;</p><p></p><span style="font-size:11.0pt;line-height:107%;font-family:&quot;Calibri&quot;,sans-serif;
mso-ascii-theme-font:minor-latin;mso-fareast-font-family:PMingLiU;mso-fareast-theme-font:
minor-fareast;mso-hansi-theme-font:minor-latin;mso-bidi-font-family:&quot;Times New Roman&quot;;
mso-bidi-theme-font:minor-bidi;mso-ansi-language:EN-US;mso-fareast-language:
ZH-TW;mso-bidi-language:AR-SA">Propensity score matching by age, sex, index
date (month), diabetes mellitus, CKD, smoking-related diagnoses, alcohol use
disorders, chronic cystitis, morbid obesity, gout, spinal cord injury and
chronic liver disease.</span></pre>

非常感谢〜!!!

0 个答案:

没有答案