Guest User

Untitled

a guest
Jan 23rd, 2018
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1.  
  2. Function.prototype.method = function(name, method) {
  3. if (!this.prototype[name]) {
  4. this.prototype[name] = method;
  5. return this;
  6. }
  7. }
  8. String.method('entityify', function () {
  9. var character = {
  10. '<' : '<',
  11. '>' : '>',
  12. '&' : '&',
  13. '"' : '"'
  14. };
  15. return function () {
  16. return this.replace(/[<>&"]/g, function (c) {
  17. return character[c];
  18. });
  19. };
  20. }());
  21. var text = '<html><body bgcolor=linen><p>' +
  22. 'This is <b>bold<\/b>!<\/p><\/body><\/html>';
  23. var tags = /[^<>]+|<(\/?)([A-Za-z]+)([^<>]*)>/g;
  24. var a, i;
  25.  
  26. while ((a = tags.exec(text))) {
  27. for (i = 0; i < a.length; i += 1) {
  28. console.log(('// [' + i + '] ' + a[i]).entityify());
  29. }
  30. }
Add Comment
Please, Sign In to add comment