简体   繁体   中英

javascript regex to extract anchor text, URL, and target from anchor tags

I am attempting to extract the parts (URL, target, text) of an anchor as I loop through a Json response and am unable to do so.

I found this question/answer which got me 95% of the way there:

javascript regex to extract anchor text and URL from anchor tags

var input_content = "blah \
    <a href=\"http://yahoo.com\">Yahoo</a> \
    blah \
    <a href=\"http://google.com\">Google</a> \
    blah";

var matches = [];

input_content.replace(/[^<]*(<a href="([^"]+)">([^<]+)<\/a>)/g, function () {
    matches.push(Array.prototype.slice.call(arguments, 1, 4));
});

alert(matches.join("\n"));

//Gives

//<a href="http://yahoo.com">Yahoo</a>,http://yahoo.com,Yahoo
//<a href="http://google.com">Google</a>,http://google.com,Google

I have not been able to modify the above regex to grab the target. Any help would be appreciated.

Thanks.

I'm not sure you have access to jQuery (also this is probably slower than a native regex) but you could extract the markup string from the JSON response and wrap it in jQuery for easy human-readable processing:

$links.find('a').each(function(){
   var text = $(this).text();
   var target = $(this).attr('target');
   var href = $(this).attr('href');

   // Do whatever you were going to do
});

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