How to replace links in proper href tags


Monney

I have six hreftabs body. I selected all links and modified all of them. Now I want to use the modified link instead. My question is how to replace the link hrefin the exact tag

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:[email protected]">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>

Matt Ellen

I recommend replacing the text with a regular expression like this:

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:[email protected]">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>

Decompose the regular expression /(\?|&)elqTrackId=[a-z0-9]+(&elqTrack=true)?/:

  • (\?|&): find ?or &. ?is a special character, so it must be escaped to be used as is.
  • elqTrackId=[a-z0-9]+: Find elqTrackId=followed by any number of lowercase letters a to z or 0 to 9
  • (elqTrack=true)?: also matches elqTrack=trueif present . Parentheses create a capturing group, and ?the average matches 0 or 1 occurrence of that group.

Related


How to replace links in proper href tags

Monney I have six hreftabs body. I selected all links and modified all of them. Now I want to use the modified link instead. My question is how to replace the link hrefin the exact tag var links = document.getElementsByTagName('a'); var new_str=[]; var new

How to replace links in proper href tags

Monney I have six hreftabs body. I selected all links and modified all of them. Now I want to use the modified link instead. My question is how to replace the link hrefin the exact tag var links = document.getElementsByTagName('a'); var new_str=[]; var new

How to replace links in proper href tags

Monney I have six hreftabs body. I selected all links and modified all of them. Now I want to use the modified link instead. My question is how to replace the link hrefin the exact tag var links = document.getElementsByTagName('a'); var new_str=[]; var new

Replace tags with href links

for J9 I have an html source and want to replace all tags with the href links it contains .<a> So the label looks like: <a href="http://google.com" target="_blank">click here</a> I expect as output: http://google.com I've tried some regex in conjunction with

How to replace href links in Textarea with jQuery

radical action I have a basic question about jQuery. I really want to change the "a" tag in Textarea. I know "a" tags don't work inside a textarea, so I can't use jQuery selectors to target them. example: <textarea><a href="http://www.google.com"></a></textare

How to replace href links in text using jQuery

Alpha reticulum cells HTML example: <p><a href='link1' target='_blank'><font color='blue' size='4'>TEXT 1</font></a></p> <br /> <p><a href='link2' target='_blank'><font color='blue' size='4'>TEXT 2</font></a></p> <br /> <p><a href='link3' target='_

How to replace href links in Textarea with jQuery

radical action I have a basic question about jQuery. I really want to change the "a" tag in Textarea. I know "a" tags don't work inside a textarea, so I can't use jQuery selectors to target them. example: <textarea><a href="http://www.google.com"></a></textare

How to replace href links in text using jQuery

Alpha reticulum cells HTML example: <p><a href='link1' target='_blank'><font color='blue' size='4'>TEXT 1</font></a></p> <br /> <p><a href='link2' target='_blank'><font color='blue' size='4'>TEXT 2</font></a></p> <br /> <p><a href='link3' target='_

How to find and replace href values on links using AngleSharp?

Rays I have some HTML snippets that contain some links with hrefs that start with a pound sign like this <a href="#Getting Started">Getting Started</a> I'm new to AngleSharp and am trying to use it to find these links and replace the href with the new value,

How to find and replace href values on links using AngleSharp?

Rays I have some HTML snippets that contain some links with hrefs that start with a pound sign like this <a href="#Getting Started">Getting Started</a> I'm new to AngleSharp and am trying to use it to find these links and replace the href with the new value,

How to find and replace href values on links using AngleSharp?

Rays I have some HTML snippets that contain some links with hrefs that start with a pound sign like this <a href="#Getting Started">Getting Started</a> I'm new to AngleSharp and am trying to use it to find these links and replace the href with the new value,

How to find and replace href values on links using AngleSharp?

Rays I have some HTML snippets that contain some links with hrefs that start with a pound sign like this <a href="#Getting Started">Getting Started</a> I'm new to AngleSharp and am trying to use it to find these links and replace the href with the new value,

How to find and replace href values on links using AngleSharp?

Rays I have some HTML snippets that contain some links with hrefs that start with a pound sign like this <a href="#Getting Started">Getting Started</a> I'm new to AngleSharp and am trying to use it to find these links and replace the href with the new value,

How to find and replace href values on links using AngleSharp?

Rays I have some HTML snippets that contain some links with hrefs that start with a pound sign like this <a href="#Getting Started">Getting Started</a> I'm new to AngleSharp and am trying to use it to find these links and replace the href with the new value,

Replace http links with anchor tags

Joel So I'm building a chat client and I read incoming messages via json. However, I would like to be able to convert the URL into a clickable anchor tag. I've looked around but haven't really found any posts asking or getting an answer. Here is my current cod

Replace http links with anchor tags

Joel So I'm building a chat client and I read incoming messages via json. However, I would like to be able to convert the URL into a clickable anchor tag. I've looked around but haven't really found any posts asking or getting an answer. Here is my current cod

Replace http links with anchor tags

Joel So I'm building a chat client and I read incoming messages via json. However, I would like to be able to convert the URL into a clickable anchor tag. I've looked around but haven't really found any posts asking or getting an answer. Here is my current cod

Replace http links with anchor tags

Joel So I'm building a chat client and I read incoming messages via json. However, I would like to be able to convert the URL into a clickable anchor tag. I've looked around but haven't really found any posts asking or getting an answer. Here is my current cod

PHP - Search and replace tags with links

Kelly I'm a bit new to PHP development, so I'm just trying some basic stuff at the moment. I have a PHP website with several pages. I want to create a script that connects anything with a hashtag I write to link to Twitter. So, for example, if I have a page wi

Replace http links with anchor tags

Joel So I'm building a chat client and I read incoming messages via json. However, I would like to be able to convert the URL into a clickable anchor tag. I've looked around but haven't really found any posts asking or getting an answer. Here is my current cod

Replace http links with anchor tags

Joel So I'm building a chat client and I read incoming messages via json. However, I would like to be able to convert the URL into a clickable anchor tag. I've looked around but haven't really found any posts asking or getting an answer. Here is my current cod

Replace http links with anchor tags

Joel So I'm building a chat client and I read incoming messages via json. However, I would like to be able to convert the URL into a clickable anchor tag. I've looked around but haven't really found any posts asking or getting an answer. Here is my current cod

PHP - Search and replace tags with links

Kelly I'm a bit new to PHP development, so I'm just trying some basic stuff at the moment. I have a PHP website with several pages. I want to create a script that connects anything with a hashtag I write to link to Twitter. So, for example, if I have a page wi