繁体   English   中英

JS 替换多行

[英]JS replace over multiple lines

使用 JavaScript

我想通过使用另一个包含电子邮件签名的字符串从多行字符串中删除电子邮件签名,如下所示...

const email = `<h1>Hi there</h1>
               <p>How are you?</p>
               <p>Bye</p>
               <div class="signature">
                  my number is 0343243
               </div>`

const signature = `<div class=signature>
                      my number is 0343243
                   </div>`

我想要的结果是...

<h1>Hi there</h1>
<p>How are you?</p>
<p>Bye</p>

您可以在这里使用简单的替换功能。 这是您的问题的解决方案。

 var value = '<h1>Hi there</h1><p>How are you?</p> <p>Bye</p><div class="signature">my number is 0343243</div>' var mask = '<div class="signature">my number is 0343243</div>' alert(value.replace(mask,''));

能够通过将多行字符串转换为单行来解决它,例如......

const singleLineEmail = email.replace(/\s+/g, ' ');
const singleLineSignature = signature.replace(/\s+/g, ' ');

从那里我能够如此简单

const cleanEmail = singleLineEmail.replace(singleLineSignature, '')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM