0

I am working on a loop to replace language tags {lang_vname} with the actual term (name)

language.js:

var lang = {
//common
   vname                   : "name",
   name                    : "lastname",
   adress                  : "adress",
   language                : "language",

replace script

function translate(output)  {
var term = output;
$.each(lang,function(i,l){
    var find = "{lang_"+i+"}";
    term = term.replace(find,l);
});
return term;}

I can't figure out how to replace the output if there is more than one expression of one kind. It's only replacing the first one and if there is a second tag of it it displays the tag. I found a solution like replace(/find/g,l); but it is not working here and stops my whole script.
Is there a way to solve that easily ?

EDIT

thanks to Felix Kling! the link he provided made it work :D my final result is

function translate(output)  {
    var term = output;
    $.each(lang,function(i,l){
        var find = "{lang_"+i+"}";
        var regex = new RegExp(find, "g");
        term = term.replace(regex, l);  
    });
    return term;
}

thanks for your fast help!

2

1 Answer 1

1

If this won't work for you please provide an example for the variable output

function translate(output)  {
    var term = output;
    $.each(lang, function(i, l){
        var find = new RegExp("{lang_"+i+"}", "g");
        term = term.replace(find, l);
    });
    return term;

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

2 Comments

something like this for example : var output = "<div class="link" id="1" url="/include/php/startseite.php">{lang_link_start}</div><div class="link subnavi" id="2">{lang_link_jobs}</div>"; its working for single expressions but as soon as there is sth twice its not replacing both
yes i combined both but the link by Felix Kling helped me to understand the issue :) thanks for your help

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.