-1

how to replace unwanted footer text?

 ---  
This email has been checked for viruses by Avast antivirus software.  
http://www.avast.com

html code footer:

     <span class="Apple-converted-space">&nbsp;</span><br><br><br>---<span class="Apple-converted-space">&nbsp;</span><br>
This email has been checked for viruses by Avast antivirus software.<span class="Apple-converted-space">&nbsp;</span><br>
<a href="http://www.avast.com" style="text-decoration: none; color: rgb(22, 54, 87); ">
http://www.avast.com</a></span></span> 

almost done, but I think the code can be placed on one line

document.getElementById('footer').innerHTML = data;
data = data.replace(/---.*/gi, '');
data = data.replace((/(<br>This email has.*).+?/gi, '');
data = data.replace((/(http:\/\/www\.avast\.com)<\/a>?(<br\s*\/?>{2,3})?/gi, '');
document.getElementById('footer').innerHTML = data;

an example found in a forum

data = data.replace(/(<br>){2,3}---(<br>This).+?(avast\.com)<\/a>( ---)?/gi, '');

the code above will workt in plain html, it won't work with the generated code by WebKit

Strike01
  • 3
  • 2
  • *"but I think the code can be placed on one line"* - Why does it need to be on one line? Why do you set innerHTML on the first line when `data` doesn't have a value yet? Are you aware that you are missing a semicolon on the third line? Why can't you get rid of the line server side? Are you aware that the example of the forum uses a `data` variable too, in other words, that that code is incomplete? – GolezTrol Jan 04 '15 at 13:49
  • in your example html, what are the last 2 `` for? Are those a mistakeor are they really in your html like that? – Wesley Smith Jan 04 '15 at 16:22
  • @GolezTrol, see example data = data.replace(/(
    ){2,3}---(
    This).+?(avast\.com)<\/a>( ---)?/gi, ''); the semicolon was a bad copy & paste it's just a part of the code: The code works accept the
    part won't replace, that's because the WebKit I think it can be improved BTW it's not on a server site but a local program. Thnx for your reply.
    – Strike01 Jan 04 '15 at 16:27
  • @DelightedD0D they really in the html like that, just edited the post, now you see the beginning as well (the start from 'spam' footer) – Strike01 Jan 04 '15 at 16:37
  • hmm, thats wierd, where are the opening tags for those two spans? – Wesley Smith Jan 04 '15 at 16:43

2 Answers2

0

The regex:

---.*|<br>\s*This email has.*|<a\b[^>]*>.*?(http:\/\/www\.avast\.com).*?</a>
  • ---.* ==> Matches text from: data.replace(/---.*/gi, '');

  • <br>\s*This email has.* ==> Matches text from: data.replace((/(<br>This email has.*).+?/gi, ''); //With an correction to match whitespace characters after <br>
    As alternative you could write: <br>\s*This email has[^<]* to stop at the end of the line or at the beginning of the next tag.

  • <a\b[^>]*>.*?http:\/\/www\.avast\.com.*?</a> ==> Matches the link that contains "http://www.avast.com" in the name (link text).

  • The | is for matching the alternative regex, if the previous regex didn't match

Regex Test


result = text.replace(/---.*|<br>\s*This email has.*|<a\b[^>]*>.*?(http:\/\/www\.avast\.com).*?<\/a>/gi, "");
Andie2302
  • 4,825
  • 4
  • 24
  • 43
0

/(<br>){3}[\s\S]+?avast.com<\/a>/ Should work just fine for you:

  1. (<br>){3} matches exactly 3 br tags
  2. [\s\S] matches anything, including line breaks (basically says any character that is a space and any character that is not a space, ie everything)
  3. + says to match one or more of the previous criteria (any character)
  4. ? says to match as few of the last criteria (any character) as necessary to reach the next criteria
  5. avast.com<\/a>/ match this string exactly

function removeFooter(){

  var elm = document.getElementById('footer');
  elm.innerHTML = elm.innerHTML.replace(/(<br>){3}[\s\S]+?avast.com<\/a>/, "repalced footer text");

}
#msgBody{
  height:55px;
  width:300px;
  background-color:#999999;
  margin-top:10px;
  
  }
<input type="button" onclick="removeFooter()" value="Remove Footer"/>
<div id="msgBody"></div>
<div id="footer">
<br><br><br>
 <span class="Apple-converted-space">&nbsp;</span><br>
  This email has been checked for viruses by Avast antivirus software.
<span class="Apple-converted-space">&nbsp;</span><br>
<a href="http://www.avast.com" style="text-decoration: none; color: rgb(22, 54, 87); "> http://www.avast.com</a>
</div>
Wesley Smith
  • 19,401
  • 22
  • 85
  • 133