javascriptregexanchornofollow

How to add rel="nofollow" to all external links that do not related to my domain with javascript?


I have the following html string with three links:

var html = '
   <a href="http://www.example.com/help">Go to help page</a>
   <a href="http://blog.example.com">Go to blog page</a>
   <a href="https://google.com">Go google</a>
';

My domain name is example.com. As you can see from the code above there is two internal links and one external.

I need to write "magic" function that adds rel="nofollow" attribute to all external links (not internal ones). So I need to get the following result:

var html = '
   <a href="http://www.example.com/help">Go to help page</a>
   <a href="http://blog.example.com">Go to blog page</a>
   <a href="https://google.com" rel="nofollow">Go google</a>
';

I'm trying to write that function and this is I have at the time:

function addNoFollowsToExternal(html) {
  // List of allowed domains
  var whiteList = ['example.com', 'blog.example.com'];

  // Regular expression
  var str = '(<a\s*(?!.*\brel=)[^>]*)(href="/https?://)((?!(?:(?:www\.)?' + whiteList.join(',') + '))[^"]+)"((?!.*\brel=)[^>]*)(?:[^>]*)>',

  // execute regexp and return result
  return html.replace(new RegExp(str, 'igm'), '$1$2$3"$4 rel="nofollow">');
}

Unfortunately my regexp seems does't work. After executing addNoFollowsToExternal(html) rel="nofollow" don't added to external link with href="https://google.com"

Please help me with fixing my regular expression to resolve my task.


Solution

  • There were some minor mistakes in your RegEx. Here is a corrected version:

    function addNoFollowsToExternal(html){
        var whiteList = ['([^/]+\.)?example.com'];
        var str = '(<a\s*(?!.*\brel=)[^>]*)(href="https?://)((?!(?:' + whiteList.join('|') + '))[^"]+)"((?!.*\brel=)[^>]*)(?:[^>]*)>';
    
        return html.replace(new RegExp(str, 'igm'), '$1$2$3"$4 rel="nofollow">');
    }