简体   繁体   中英

regular expression to add a target="blank" to all external links

Is it possible to add a target="_blank" to all <a> tags, with just one regular expression? I've been experimenting with negative and positive look aheads/behinds, to no avail. This should:

  • Match anchor tags whose href starts with http://
  • If the anchor tag does not have a target tag, adds a target="_blank" tag
  • If it does have a target tag, it checks if the target tag is not already set to "_blank" , and if not it is replaced to target="_blank"

Is this possible? If not, what would be the least computationally intensive way to do this?

You didn't really specify whether this would be the html from the DOM (among other things). Assuming you want to modify the DOM... Using jQuery, you could do something like this:

$(document).ready( function() {
    $('a').filter( function() {
        return $(this).attr('href').substr(0, 7) == "http://";
    }).attr('target', '_blank');
});

here is a jsfiddle showing it works (you can inspect the elements to see that the a 's with href 's starting with http:// have target="_blank" ) : http://jsfiddle.net/amRrj/

If the HTML is also well-formed XML then you would be better off using an XML tool with support for xpath and xslt. Take a look at XMLStarlet which provides tools similar to grep, sed etc for working with XML. I think this would do what you want, but I have not tried it:

xml ed -P -S -i //a -t attr -n target -v _blank somefile.html >somefile_2.html

xml ed invokes the XMLStarlet edit command

-P preserve formatting

-S preserve whitespace

-i //a insert at every tag

-t attr insert type is attribute

-n target name of attribute to insert

-v _blank value of attribute to insert

For more complex editing you can use XMLStarlet with an xslt transform.

Another option is adding target="_blank" to the <base href…> . This will globally affect the pages <a> tag targets rather than needing to add them individually. Target declarations on <a> tags will override this.

    var target = '_self';

    if(external){
        target = '_blank';
    }

    $('#base_tag').attr('target', target);

Fiddle

<base> Documentation

I write this one maybe it helps:

Regex: /<a(.*?)href="(.*?)"(.*?)>(.*?)<\/a>/gi

 console.log(`<a href="https://www.stackoverflow.com/test" target="_parent">Stackoverflow</a>`.replace(/<a(.*?)href="(.*?)"(.*?)>(.*?)<\/a>/gi, '<a href="$2" target="_blank">$4</a>')); console.log(`<a href="https://www.stackoverflow.com/test" target="_target">Stackoverflow</a>`.replace(/<a(.*?)href="(.*?)"(.*?)>(.*?)<\/a>/gi, '<a href="$2" target="_blank">$4</a>')); console.log(`<a href="https://www.stackoverflow.com/test">Stackoverflow</a>`.replace(/<a(.*?)href="(.*?)"(.*?)>(.*?)<\/a>/gi, '<a href="$2" target="_blank">$4</a>'));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM