JS替换多行

时间:2017-10-30 09:15:47

标签: javascript regex

使用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>

2 个答案:

答案 0 :(得分:1)

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

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,''));
 

答案 1 :(得分:0)

能够通过将多线字符串转换为单行来对其进行排序,其中包含...

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

从那时起,我能够如此简单地

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