title: How to add rel="nofollow" to all external links that do not related to my domain with javascript?
body: <p>I have the following html string with three links:</p>
  
  <pre><code>var html = '
     &lt;a href="http://www.example.com/help"&gt;Go to help page&lt;/a&gt;
     &lt;a href="http://blog.example.com"&gt;Go to blog page&lt;/a&gt;
     &lt;a href="https://google.com"&gt;Go google&lt;/a&gt;
  ';
  </code></pre>
  
  <p>My domain name is <code>example.com</code>. As you can see from the code above there is two internal links and one external.</p>
  
  <p>I need to write "magic" function that adds <code>rel="nofollow"</code> attribute to all external links (not internal ones). So I need to get the following result:</p>
  
  <pre><code>var html = '
     &lt;a href="http://www.example.com/help"&gt;Go to help page&lt;/a&gt;
     &lt;a href="http://blog.example.com"&gt;Go to blog page&lt;/a&gt;
     &lt;a href="https://google.com" rel="nofollow"&gt;Go google&lt;/a&gt;
  ';
  </code></pre>
  
  <p>I'm trying to write that function and this is I have at the time:</p>
  
  <pre><code>function addNoFollowsToExternal(html) {
    // List of allowed domains
    var whiteList = ['example.com', 'blog.example.com'];
  
    // Regular expression
    var str = '(&lt;a\s*(?!.*\brel=)[^&gt;]*)(href="/https?://)((?!(?:(?:www\.)?' + whiteList.join(',') + '))[^"]+)"((?!.*\brel=)[^&gt;]*)(?:[^&gt;]*)&gt;',
  
    // execute regexp and return result
    return html.replace(new RegExp(str, 'igm'), '$1$2$3"$4 rel="nofollow"&gt;');
  }
  </code></pre>
  
  <p>Unfortunately my regexp seems does't work. After executing <code>addNoFollowsToExternal(html)</code> <code>rel="nofollow"</code> don't added to external link with <code>href="https://google.com"</code></p>
  
  <p>Please help me with fixing my regular expression to resolve my task.</p>