Advertisement
dimipan80

Extract Element Content

Dec 14th, 2014
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Write a JavaScript function extractContent(str) that extracts the text content from given HTML fragment
  2. (given as string). The function should return anything that is in a tag, without the tags.
  3. Write JS program contentExtracter.js that invokes your function with the sample input data below
  4. and prints the output at the console. */
  5.  
  6. "use strict";
  7.  
  8. function extractContent(str) {
  9.     var pattern = />[^<]+/g;
  10.     var matcher;
  11.     var contentArr = [];
  12.     while ((matcher = pattern.exec(str)) !== null) {
  13.         var content = matcher[0].replace(/^>/, '');
  14.         contentArr.push(content);
  15.     }
  16.  
  17.     console.log(contentArr.join(''));
  18. }
  19.  
  20. extractContent("<p>Hello</p><a href='http://w3c.org'>W3C</a>");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement