1

I want to write JavaScript function strinformat like in c#.

function gg_stringformat(){
    argcount=arguments.length;
    if (argcount==0) {
        return "";
       }
    else if (argcount==1){
        return arguments[0]; 
       }
    else {
         original =arguments[0]; 
         for (var i=1; i<argcount;i++) {
            strtoreplace='{'+(i-1)+'}'; 
            strreplacewith=arguments[i];
            original=original.replace('/%' + strtoreplace + '%/gi',strreplacewith);
            }//for 
         return original;
       }
}   

when I use original=original.replace( strtoreplace , strreplacewith); it works fine but replaces only first occurence.

with code above it not works at all. what i must do?

7
  • Please share some sample inputs for calling gg_stringformat method. Commented Jan 6, 2018 at 10:13
  • gg_stringformat("My name is {0}, My age is {1}", "George Gogiava", 35) Commented Jan 6, 2018 at 10:15
  • I don't know regex. Commented Jan 6, 2018 at 10:18
  • Then you should grab a tutorial because the first parameter of .replace('/%' + strtoreplace + '%/gi', ...) uses one - at least if you fix the error shown in my duplicate. And after that you have to edit the regular expression because right now it would not match the part you want to replace. Commented Jan 6, 2018 at 10:22
  • It's duplicate question, I have found answer in referenced link. Commented Jan 6, 2018 at 10:24

3 Answers 3

1

Some annotations:

  • declare all variable at top of the function,

  • use variable names which makes sense later,

  • take a constructor for the regular expression, RegExp

  • escape the character with special meaning, like the start of a curly bracket,

    new RegExp('\\{' + (i - 1) + '}', 'gi')
    //          ^^^
    
  • exit early, dont use else by using return statements before, because this ends the function.

function gg_stringformat() {
    var argcount = arguments.length,
        string,
        i;

    if (!argcount) {
        return "";
    }
    if (argcount === 1) {
        return arguments[0];
    }
    string = arguments[0];
    for (i = 1; i < argcount; i++) {
        string = string.replace(new RegExp('\\{' + (i - 1) + '}', 'gi'), arguments[i]);
    }
    return string;
}

console.log(gg_stringformat("My name is {0}, My age is {1}", "George Gogiava", 35));

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

Comments

1

Just in case you are not OK with RegExp you can write something like this-

String.prototype.replaceAll = function(oldOne, newOne) {
        return $(this).split(oldOne).join(newOne);
    };

This will add a method to String prototype and then you an write-

original=original.replaceAll(strtoreplace , strreplacewith);

It will replace every occurence of the string.

Comments

0

You should try creating RegExp with a constructor. Syntax is:

/pattern/flags
new RegExp(pattern[, flags])
RegExp(pattern[, flags])

function gg_stringformat(){
    argcount=arguments.length;
    if (argcount==0) {
        return "";
       }
    else if (argcount==1){
        return arguments[0]; 
       }
    else {
         original =arguments[0]; 
         for (var i=1; i<argcount;i++) {
            strtoreplace='{'+(i-1)+'}'; 
            strreplacewith=arguments[i];
            original=original.replace(new RegExp('%' + strtoreplace + '%', 'gi') ,strreplacewith);
            }//for 
         return original;
       }
} 

More information you can find here

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp

Comments

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.