Advertisement
dimipan80

Replace <a> Tag

Nov 17th, 2014
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Write a JavaScript function replaceATag(str) that replaces in a HTML document given as string
  2.  all the tags <a href="…">…</a> with corresponding tags [URL=…]…/URL]. Write JS program aTagReplacer.js
  3.  that invokes your function with the sample input data below and prints the output at the console. */
  4.  
  5. "use strict";
  6.  
  7. function replaceATag(str) {
  8.     var pattern = /<a\s([^>]+)>([^<]+)<\/a>/g;
  9.     var matcher = pattern.exec(str);
  10.     var replacedString = '[URL ' + matcher[1] + ']' + matcher[2] + '[/URL]';
  11.     console.log(str.replace(matcher[0], replacedString));
  12. }
  13.  
  14. replaceATag(
  15.     "<ul>" +
  16.     "<li>" +
  17.     "<a href = http://softuni.bg>SoftUni</a>" +
  18.     "</li>" +
  19.     "</ul>");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement