Use javascript regex to extract anchor text and URL from anchor tags - Javascript RegExp

Javascript examples for RegExp:RegExp test

Description

Use javascript regex to extract anchor text and URL from anchor tags

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=function(){/*from   w  w  w . ja va 2  s  .  c om*/
var text = 'This is my <a target="_blank" href="www.your.co.uk?Test=t+1">link</a> Text';
var urlPattern = /([^+>]*)[^<]*(<a [^>]*(href="([^>^\"]*)")[^>]*>)([^<]+)(<\/a>)/gi;
var output = text.replace(urlPattern, "$1___$2___$3___$4___$5___$6");
console.log(output);
    }

      </script> 
   </head> 
   <body>  
   </body>
</html>

Related Tutorials