개발/Javascript

Javascript C# String.Format 메서드와 동일한 기능

혈중마라농도 2022. 1. 18. 21:12
// C# String.Format 메서드와 동일하게 동작시키기 위한 함수
String.format = function () {
    // The string containing the format items (e.g. "{0}")
    // will and always has to be the first argument.
    var theString = arguments[0];

    // start with the second argument (i = 1)
    for (var i = 1; i < arguments.length; i++) {
        // "gm" = RegEx options for Global search (more than one instance)
        // and for Multiline search
        var regEx = new RegExp("\\{" + (i - 1) + "\\}", "gm");
        theString = theString.replace(regEx, arguments[i]);
    }
    return theString;
}

 

 

반응형