Does string replace characters?

I have an object like this:

myDataObject = {
    name : 'Nikola Tesla',
    birth : ['10 July 1856','10. Juli 1856'],
    nation : ['Serbian','Serbisch'],
    knownFor : ['Alternating current',' Zweiphasenwechselstrom']
}

And two string patterns like these:

var englishStr = '#name#, born #birth[1]# , #nation[1]# best known for his contributions to #knownFor[1]#';

var deutschStr = '#name#, geboren #birth[2]#, #nation[2]# Erfinder, der für seine Beiträge zur #knownFor[2]# bekannt';

Now I want to replace #properties#in this way.

I could do it easily if there weren’t a multilingual indicator like [1] or [2]. similar to this:

$.each(myDataObject , function(n, v){
    englishStr = englishStr.replace('#'+ n +'#' , v )
});

So what can I do with #prop[i]#? Thanks you

+3
source share
5 answers

One path would move from a row to a data object, rather than through all keys.

var myDataObject = {
    name : 'Nikola Tesla',
    birth : ['10 July 1856','10. Juli 1856'],
    nation : ['Serbian','Serbisch'],
    knownFor : ['Alternating current',' Zweiphasenwechselstrom']
};
var englishStr = "#name#, born #birth[1]# , #nation[1]# best known for his contributions to  #knownFor[1]#";




var re = /#([^\[#]+)\[?(\d+)?\]?#/;  //Looks for #STRING# or #STRING[NUMBER]#
var test;
while ( (test=re.exec(englishStr))!==null) {  //Keep looking for matches in the string
    var key = test[1];   //get the key to the object
    var index = test[2]; //get the index if there
    var item = myDataObject[key];  //get reference to the item in the object
    if (index!==undefined && item) {  //if we have an index, look up the value from array
        index = parseInt(index,10)-1;  //arrays are zero index, so need to subtract one
        item = item[index];  //get the string
    }
    englishStr = englishStr.replace(re, item || "N/A");  //make the replacement in the string with the data from the object
};
+3
source

I suggest you try a slightly different approach. Instead of looping around myDataObjectblindly trying to replace the values, first pull out the values ​​you want to replace, and then replace them with their values.

var regex = /#(.*?)(?:\[(\d*)])?#/g;
while(match = regex.exec(englishStr)){
    var matchStr = match[0];
    var data = myDataObject[match[1]];
    if(match[2] !== undefined){
        data = data[match[2] - 1];
    }

    englishStr = englishStr.replace(matchStr, data);
}

DEMO: http://jsfiddle.net/35jZM/

+1

,

$.each(myDataObject , function(n, v){

    if(typeof v == 'object'){
        $.each(v , function(index, value){
            englishStr = englishStr.replace('#'+ n +'[' + index + ']' + '#' , v[index-1]    );

        });
   }
englishStr = englishStr.replace('#'+ n +'#' , v )

});

+1

, - :

myDataObject = {
    name: 'Nikola Tesla',
    birth: ['10 July 1856', '10. Juli 1856'],
    nation: ['Serbian', 'Serbisch'],
    knownFor: ['Alternating current', ' Zweiphasenwechselstrom']
};

// English: 0, Deutsch: 1
var language = 0;

var str = "#name#, born #$birth# , #$nation# best known for his contributions to  #$knownFor#";

$.each(myDataObject, function (n, v) {
    str = str.replace('#' + n + '#', v);
    str = str.replace('#$' + n + '#', v[language]);
});

alert(str);

Fiddle

+1
source

You can also pass capture groups to replace the regular expression with a callback function:
(note that if arrays in your translation strings are indexed from 1, you need to add this to $2)

englishStr.replace(/#(.+?)(?:\[(\d+)\])?#/g, function($0, $1, $2){ 
    return $2 === undefined ? myDataObject[$1] : myDataObject[$1][$2];
});

regex visualization

Demo

+1
source

All Articles