Last active
December 28, 2015 20:39
-
-
Save Rplus/7559318 to your computer and use it in GitHub Desktop.
自幹型tpl似乎還需要調整
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// escape unsafety chart | |
// via: http://stackoverflow.com/a/12034334 | |
window.entityMap = { | |
'<': '<', | |
'>': '>', | |
'"': '"', | |
'\'': ''', | |
'/': '/' | |
}; | |
// 將 str(template) 中特定字串(#{$str})以 物件式 批次取代傳回 | |
window.replaceTpl = function(str, obj) { | |
var pattern = /\#\{\$(.+?)\}/g; | |
return str.replace(pattern, function(match) { | |
return String(obj[match.replace(pattern, '$1')]).replace(/[<>"'\/]/g, function(s) { | |
return window.entityMap[s]; | |
}); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
加了 escape 的 string map