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?

GGSoft
  • 439
  • 6
  • 15
  • Please share some sample inputs for calling `gg_stringformat` method. – gurvinder372 Jan 06 '18 at 10:13
  • gg_stringformat("My name is {0}, My age is {1}", "George Gogiava", 35) – GGSoft Jan 06 '18 at 10:15
  • I don't know regex. – GGSoft Jan 06 '18 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. – Andreas Jan 06 '18 at 10:22
  • It's duplicate question, I have found answer in referenced link. – GGSoft Jan 06 '18 at 10:24
  • @andreas I have replaced line with `var re = new RegExp(strtoreplace,"g"); original=original.replace(re,strreplacewith);` , but it stil not works. please give me a right syntax. – GGSoft Jan 06 '18 at 10:33
  • the code hase more quirks than just the regular expression. – Nina Scholz Jan 06 '18 at 10:35

3 Answers3

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));
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
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.

Hq Shiblu
  • 91
  • 5
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

Denis Rybalka
  • 1,821
  • 3
  • 18
  • 28