JavaScript equivalent to printf/String.Format

前端 未结 30 2395
囚心锁ツ
囚心锁ツ 2020-11-21 04:27

I\'m looking for a good JavaScript equivalent of the C/PHP printf() or for C#/Java programmers, String.Format() (IFormatProvider for .

30条回答
  •  你的背包
    2020-11-21 05:27

    Just in case someone needs a function to prevent polluting global scope, here is the function that does the same:

      function _format (str, arr) {
        return str.replace(/{(\d+)}/g, function (match, number) {
          return typeof arr[number] != 'undefined' ? arr[number] : match;
        });
      };
    

提交回复
热议问题