This file contains 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); |
で、式展開ってこれでいいのかな?
気になってるのは該当のする#{}がすべて置き換わった後もループを続けてしまうところ。
まだまだブラッシュアップできる気がするなぁ。