Advertisement
dimipan80

Exams - Extract Hyperlinks

Dec 17th, 2014
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Write a JavaScript function to extract all hyperlinks (<href=…>) from given text.
  2.  The text comes as array of strings, passed as parameter to your function. Print at the console
  3.  the href values in the text. The input text is standard HTML code. It may hold many tags and can be formatted
  4.  in many different forms (with or without whitespace). The <a> elements may have many attributes, not only href.
  5.  You should extract only the values of the href attributes of all <a> elements.
  6.  Print at the console the href values in the text, each at a separate line, in the order they come from the input.
  7.  The input will be well formed HTML fragment (all tags and attributes will be correctly closed).
  8.  Attribute values will never hold tags and hyperlinks, e.g. "<img alt='<a href="hello">' />" is invalid.
  9.  Commented links are also extracted. */
  10.  
  11. "use strict";
  12.  
  13. function hyperlinksExtractor(args) {
  14.     var htmlCode = args.join('\n');
  15.     var pattern = /<a\s+([^>]+\s+)?href\s*=\s*('([^']*)'|"([^"]*)|([^\s>]+))[^>]*>/g;
  16.     var matcher;
  17.     while (matcher = pattern.exec(htmlCode)) {
  18.         console.log(matcher[3] ? matcher[3] : matcher[4] ? matcher[4] : matcher[5]);
  19.     }
  20. }
  21.  
  22. hyperlinksExtractor(['<a href="http://softuni.bg" class="new"></a>']);
  23.  
  24. hyperlinksExtractor(['<p>This text has no links</p>']);
  25.  
  26. hyperlinksExtractor([
  27.     '<!DOCTYPE html>',
  28.     '<html>',
  29.     '<head>',
  30.     '  <title>Hyperlinks</title>',
  31.     '  <link href="theme.css" rel="stylesheet" />',
  32.     '</head>',
  33.     '<body>',
  34.     '<ul><li><a   href="/"  id="home">Home</a></li><li><a',
  35.     ' class="selected" href="/courses">Courses</a>',
  36.     '</li><li><a href = ',
  37.     '\'/forum\' >Forum</a></li><li><a class="href"',
  38.     'onclick="go()" href= "#">Forum</a></li>',
  39.     '<li><a id="js" href =',
  40.     '"javascript:alert(\'hi\')" class="new">click</a></li>',
  41.     '<li><a id=\'nakov\' href =',
  42.     '\'http://www.nakov.com\' class=\'new\'>nak</a></li></ul>',
  43.     '<a href="#"></a>',
  44.     '<a id="href">href=\'fake\'<img src=\'http://abv.bg/i.gif\' ',
  45.     'alt=\'abv\'/></a><a href="#">&lt;a href=\'hello\'&gt;</a>',
  46.     '<!-- This code is commented:',
  47.     '  <a href="#commented">commentex hyperlink</a> -->',
  48.     '</body>'
  49. ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement