Advertisement
ralitsa_d

UrlParser

Jan 16th, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.  
  3.     let pattern = /<a [^>]+>([^<]+)<\/a>/g;
  4.  
  5.     let matches = input[0].match(pattern);
  6.  
  7.     for (let match of matches){
  8.         let hrefPattern = /"((?:\\.|[^"\\])*)"/g;
  9.         let href = match.match(hrefPattern)[0];
  10.         href = href.substr(1, href.length - 2);
  11.  
  12.         let startIndex = match.indexOf('>', 1) + 1;
  13.         let endIndex = match.indexOf('<', 1);
  14.  
  15.         let link = match.substring(startIndex, endIndex);
  16.  
  17.         let result = `[${link}](${href})`;
  18.  
  19.         console.log(match);a
  20.         console.log(result);
  21.  
  22.         input[0].replace()
  23.     }
  24.  
  25.  
  26.  
  27.     console.log(input[0]);
  28. }
  29.  
  30. solve(['<p>Please visit <a href="http://academy.telerik.com">our site</a> to choose a training course. Also visit <a href="www.devbg.org">our forum</a> to discuss the courses.</p>']);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement