如何替换适当的href标签中的链接

时间:2019-11-08 09:22:34

标签: javascript html

我在href中有六个body标签。我选择了所有链接并修改了所有这些链接。现在我想用修改后的链接代替。我的问题是如何替换确切的href标签

中的链接

var links = document.getElementsByTagName('a');
  var new_str=[];
  var new_utm=[];
  var arr =[];
  var utm_link_with_token = '&utm_source=iconnect&utm_medium=email&utm_campaign={{Approved_Document_vod__c.Vault_Document_ID_vod__c}}&emailId={{Approved_Document_vod__c.Vault_Document_ID_vod__c}}&codsId={{Account.CODS_External_Id__c}}&utm_term=';
  var utm_link_without_token = '?utm_source=iconnect&utm_medium=email&utm_campaign={{Approved_Document_vod__c.Vault_Document_ID_vod__c}}&emailId={{Approved_Document_vod__c.Vault_Document_ID_vod__c}}&codsId={{Account.CODS_External_Id__c}}&utm_term='

  for(var i = 0; i< links.length; i++){
      arr.push(links[i].href);
  }
  console.log(arr);


  for(var i = 0; i< arr.length; i++){

      if ( (arr[i].indexOf("elqTrackId") > 0) ) {
        
          if ( (arr[i].indexOf("?elqTrackId") > 0) ) {
              new_str[i] = arr[i].substring(0, arr[i].indexOf("?elqTrackId"));
              if ((new_str[i].indexOf("?ea=") > 0) ){
                  new_utm[i] = new_str[i].concat(utm_link_with_token);
              }
              else{
                  new_utm[i] = new_str[i].concat(utm_link_without_token);
              }
          }
          else if ( (arr[i].indexOf("&elqTrackId") > 0) ) {
              new_str[i] = arr[i].substring(0, arr[i].indexOf("&elqTrackId"));
              if ((new_str[i].indexOf("?ea=") > 0) ){
                  new_utm[i] = new_str[i].concat(utm_link_with_token);
              }
              else{
                  new_utm[i] = new_str[i].concat(utm_link_without_token);
              }
          }
      }
      else if ( (arr[i].indexOf("mailto:") >= 0) || (arr[i].indexOf("tel:") >= 0) ) {
         new_str[i] = arr[i];
         new_utm[i] = new_str[i];
        
      }
      else{
          new_str[i] = arr[i];
          if ((new_str[i].indexOf("?ea=") > 0) ){
              new_utm[i] = new_str[i].concat(utm_link_with_token);
          }
          else{
              new_utm[i] = new_str[i].concat(utm_link_without_token);
          }
      }
  }
  console.log("link showed without eloqua tracking id", new_utm);
<!DOCTYPE html><html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office">

<body width="100%" style="margin: 0; padding: 0 !important; mso-line-height-rule: exactly; background-color: #f4f4f4; width: 100% !important;">
  <center style="width: 100% !important; background-color: #f4f4f4;">
    <table cellpadding="0" cellspacing="0" border="0" width="100%" bgcolor="#f4f4f4" style="width:100% !important;">
      <tbody><tr>
        <td align="center" valign="top" class="">
          <table class="container" cellpadding="0" cellspacing="0" border="0" width="600" bgcolor="#ffffff" style="margin: 0 auto;">
            <!-- TEXT HEADER SECTION STARTS -->
            <tbody>

              <tr>
                <td bgcolor="#ffffff" style="-moz-osx-font-smoothing: grayscale; -webkit-font-smoothing: antialiased; border-collapse: collapse; color: #717171; font-family: 'Verdana', 'Arial', 'Helvetica', sans-serif; font-size: 12px; font-smoothing: antialiased; font-weight: normal; line-height: 20px; margin: 0; padding: 0; padding: 10px; text-align: left; vertical-align: top; word-spacing: 2px; border: 2px solid #4089c3;">
                  <a href="https://www.google.com/dummy0?ea=8mYoHZtET8c&elqTrackId=a3a008f9213248fe8bb899351706a6ad">Lorem Ipsum</a> is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the <a href="mailto:dummy@gmail.com">industry's</a> standard dummy text ever since the 1500s, when an unknown printer took a galley of type <a href="tel:012414 258">and</a> scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into <a href="http://www.google.com/dummy1?elqTrackId=2cdd5998b5024a23b2d21839786d24a8&elqTrack=true">electronic</a> typesetting, <a href="http://www.google.com/dummy2&elqTrackId=2cdd5998b5024a23b2d21839786d24a8&elqTrack=true">remaining</a> essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently <a href="http://www.google.com/dumm3">with</a> desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
                </td>
              </tr>

           
          </tbody></table>
        </td>
      </tr>
    </tbody></table>
  </center>

</body></html>

1 个答案:

答案 0 :(得分:0)

我建议使用正则表达式替换文本,例如:

let links = document.getElementsByTagName('a');

for(let link of links)
{
  let curhref = link.href;
  if(curhref.indexOf('http') > -1)
  {
    link.href = curhref.replace(/(\?|&)elqTrackId=[a-z0-9]+(&elqTrack=true)?/, '');
    if(link.href.indexOf('ea=') > -1)
    {
      link.href += '&utm_source=iconnect&utm_medium=email&utm_campaign={{Approved_Document_vod__c.Vault_Document_ID_vod__c}}&emailId={{Approved_Document_vod__c.Vault_Document_ID_vod__c}}&codsId={{Account.CODS_External_Id__c}}&utm_term=';
    }
    else
    {
      link.href += '?utm_source=iconnect&utm_medium=email&utm_campaign={{Approved_Document_vod__c.Vault_Document_ID_vod__c}}&emailId={{Approved_Document_vod__c.Vault_Document_ID_vod__c}}&codsId={{Account.CODS_External_Id__c}}&utm_term=';
    }
  }
}
<center style="width: 100% !important; background-color: #f4f4f4;">
    <table cellpadding="0" cellspacing="0" border="0" width="100%" bgcolor="#f4f4f4" style="width:100% !important;">
      <tbody><tr>
        <td align="center" valign="top" class="">
          <table class="container" cellpadding="0" cellspacing="0" border="0" width="600" bgcolor="#ffffff" style="margin: 0 auto;">
            <!-- TEXT HEADER SECTION STARTS -->
            <tbody>

              <tr>
                <td bgcolor="#ffffff" style="-moz-osx-font-smoothing: grayscale; -webkit-font-smoothing: antialiased; border-collapse: collapse; color: #717171; font-family: 'Verdana', 'Arial', 'Helvetica', sans-serif; font-size: 12px; font-smoothing: antialiased; font-weight: normal; line-height: 20px; margin: 0; padding: 0; padding: 10px; text-align: left; vertical-align: top; word-spacing: 2px; border: 2px solid #4089c3;">
                  <a href="https://www.google.com/dummy0?ea=8mYoHZtET8c&elqTrackId=a3a008f9213248fe8bb899351706a6ad">Lorem Ipsum</a> is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the <a href="mailto:dummy@gmail.com">industry's</a> standard dummy text ever since the 1500s, when an unknown printer took a galley of type <a href="tel:012414 258">and</a> scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into <a href="http://www.google.com/dummy1?elqTrackId=2cdd5998b5024a23b2d21839786d24a8&elqTrack=true">electronic</a> typesetting, <a href="http://www.google.com/dummy2&elqTrackId=2cdd5998b5024a23b2d21839786d24a8&elqTrack=true">remaining</a> essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently <a href="http://www.google.com/dumm3">with</a> desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
                </td>
              </tr>

           
          </tbody></table>
        </td>
      </tr>
    </tbody></table>
  </center>

破坏正则表达式/(\?|&)elqTrackId=[a-z0-9]+(&elqTrack=true)?/

  • (\?|&):查找?&?是一个特殊字符,因此必须先转义才能使用。
  • elqTrackId=[a-z0-9]+:查找elqTrackId=,后跟任意数量的小写字母a至z或0至9
  • (elqTrack=true)?:如果有elqTrack=true,也要匹配。括号创建一个捕获组,?表示匹配该组的0或1次出现。