Skip to content

Instantly share code, notes, and snippets.

@kozo002
Created January 21, 2012 16:58
Show Gist options
  • Select an option

  • Save kozo002/1653290 to your computer and use it in GitHub Desktop.

Select an option

Save kozo002/1653290 to your computer and use it in GitHub Desktop.
jQuery Expression substitution method
$.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