GGSoft
GGSoft

Reputation: 429

JavaScript string.replace doesn't works

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?

Upvotes: 1

Views: 171

Answers (3)

Hq Shiblu
Hq Shiblu

Reputation: 91

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.

Upvotes: 1

Nina Scholz
Nina Scholz

Reputation: 386624

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));

Upvotes: 1

Denis Rybalka
Denis Rybalka

Reputation: 1871

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

Upvotes: 0

Related Questions