Guest User

Untitled

a guest
Feb 21st, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.44 KB | None | 0 0
  1. /*
  2.  
  3. 'My name is #{name=Earl}'.interpolate2(); // "My name is Earl"
  4. 'My name is #{name}'.interpolate2({ name: 'Earl2D2' }); // "My name is Earl2D2"
  5.  
  6. */
  7. String.prototype.interpolate2 = (function(){
  8. var re = /#{([-\w]+)(?:=([-\w]+))?}/g;
  9. return function(source) {
  10. if (!source) source = { };
  11. return this.replace(re, function(match, name, value) {
  12. return (name in source ? source[name] : (value || ''));
  13. })
  14. }
  15. })();
Add Comment
Please, Sign In to add comment