Created
January 21, 2012 16:58
-
-
Save kozo002/1653290 to your computer and use it in GitHub Desktop.
jQuery Expression substitution method
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
| $.expsub = function(str, data) { | |
| if (typeof str === 'string' && typeof data !== 'undefined' && typeof data === 'object') { | |
| $.each(str.match(/(#{(?:.*?)})/g), function(i, match) { | |
| replace = data[match.replace(/^#{|}$/g, '')]; | |
| if (typeof replace === 'function') replace = replace(); | |
| str = str.replace(RegExp(match), replace); | |
| }); | |
| return str; | |
| } | |
| } | |
| var sample_str = '私もほかすでにその#{one}といったのの時が云っうませ。人知れず当時が説明人は何しろ大した#{two}たただけにしのにいますには帰着すれましですと'; | |
| var sample_data = {one:'活動家', two:function(){ return '尊敬' }}; | |
| var result = $.expsub(sample_str, sample_data); | |
| console.log(result); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment