Advertisement
Guest User

Untitled

a guest
Feb 10th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. Function.prototype.method = function (name, func) {
  2. this.prototype[name] = func;
  3. return this;
  4. };
  5.  
  6. String.method('deentityify', function () {
  7. // The entity table. It maps entity names to
  8. // characters.
  9. var entity = {
  10. quot: '"',
  11. lt: '<',
  12. gt: '>'
  13. };
  14. return function () {
  15. // This is the deentityify method. It calls the string
  16. // replace method, looking for substrings that start
  17. // with '&' and end with ';'. If the characters in
  18. // between are in the entity table, then replace the
  19. // entity with the character from the table. It uses
  20. // a regular expression (Chapter 7).
  21. return this.replace(/&([^&;]+);/g,
  22. function (a, b) {
  23. var r = entity[b];
  24. return typeof r === 'string' ? r : a;
  25. }
  26. );
  27. };
  28. }());
  29.  
  30. '<">'.deentityify()
  31.  
  32. String.method('deentityify', function () {
  33. // The entity table. It maps entity names to
  34. // characters.
  35. var entity = {
  36. quot: '"',
  37. lt: '<',
  38. gt: '>'
  39. };
  40. return function () {
  41. // This is the deentityify method. It calls the string
  42. // replace method, looking for substrings that start
  43. // with '&' and end with ';'. If the characters in
  44. // between are in the entity table, then replace the
  45. // entity with the character from the table. It uses
  46. // a regular expression (Chapter 7).
  47. return this.replace(/&([^&;]+);/g,
  48. function (a, b) {
  49. var r = entity[b];
  50. return typeof r === 'string' ? r : a;
  51. }
  52. );
  53. };
  54. }());
  55.  
  56. 'any string'.deentityfy()
  57.  
  58. String.deentityfy()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement