Replacing custom variables using jQuery and regex -


i attempting replace custom variable tags in content block values array. have working code replacing variables %([variable_name])% however, need replace variables <%[variable_name]%>. have been messing around following code few hours, , i'm having trouble wrapping head around regex portions, , getting work correctly new variable format.

here code works %([variable_name])%, how can work <%[variable_name]%>?

$.each(data, function(key, value){     var our_key = "%(["+key+"])%";     our_key = our_key.replace(/([.?*+^$[\]\\(){}|-])/g, "\\$1");     var regex = new regexp(our_key, 'g');     new_contents = new_contents.replace(regex,value); }); 

edit: real problem was grabbing "new_contents" using jquery '.html()' attribute, replacing '<' , '>' '& lt;' , '& gt;'

why not other way round?

var replacementmap = {         "name": "john doe"     },     text = "hello <%[name]%>!";  text = text.replace(/<%\[([^\]]+)\]%>/g, function(match, key) {     return replacementmap[key] || match; });  console.log(text);    // "hello john doe!" 

fiddle


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -