1

I wish to do a search and replace in a string. It searches for any word that begins with "$" and replaces it with a value from an array. For example if the string is:

[div class='news'][h4]$title[/h4][p]$desc[/p][/div]

It replaces [] to <> (already done). But then i want it to replace $title with data from an array. So data["title"] and then $desc would be replaced with data["desc"]. The code i have so far is

var obj = $('#'+id);
var url = $(obj).attr('loadJSON');
var format = $(obj).attr('responseFormat');
$.getJSON(url, function(data) {
    var html = "";
    for(var i=0;i<data.length;i++) {
        var tmp = format;
        tmp = tmp.replace(/\[+(.*?)\]+/g,"<$1>");
        tmp = tmp.replace();
    }
});

The format is the string which it will replace in, and data (from the JSON response) is the array which i want the variables to change to.

Could someone please help me with this? Thanks in advance

3 Answers 3

4

then add as last replacement

tmp = tmp.replace(/\$([a-z]+)/gi, function(match, v) {
   return data[v] || v;
})

note that in case of data[v] is undefined you could return something else like data[v] || ["not found", v].join(' ') just to track what variable is missing

Sign up to request clarification or add additional context in comments.

2 Comments

as aside suggestion you could use {$var} instead, since is a wider used sytntax in microtemplating
I would do, but the html files are generated from PHP first, using a template system, which already uses {$var} so i had to use something slightly different. Thanks for the suggestion though
0

I'm no JS expert, but this looks wrong in so many ways... your data object should be a JS object, that is something like { title: 'Generic title.', description: 'It's a generic title' }.

Why not put the values into paragraph elements, and then insert those into the div (i.e., you append them). Something like this:

$.getJSON(url, function(data) {
    $('div.news').append($('<p />').text(data.title));
    $('div.news').append($('<p />').txt(data.description));
});

1 Comment

The data object is a JS object, its loaded via AJAX from an external source, and i wanted this in a function which could be easily intergrated to format external content in any conatainer. So for example, i could use the same JS function to load data and insert it into a table
0

You could do

tmp = tmp.replace(/\$([a-z]+)/gi, function(match) {
    match = match.replace('$', '');
   return (data[match] || match)
});

http://jsfiddle.net/uZbk8/

2 Comments

if you're using a capturing group, you may just pass another parameter to the callback and avoid an inner replacement
@FabrizioCalderan Yes i saw (and upvoted) your answer, it's much better! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.