Nodejs String Format format()

Here you can find the source of format()

Method Source Code

////ww  w  .  jav a 2s  .com
// .net string.format like function
// usage:   "{0} means 'zero'".format("nula") 
// returns: "nula means 'zero'"
// place holders must be in a range 0-99.
// if no argument given for the placeholder, 
// no replacement will be done, so
// "oops {99}".format("!")
// returns the input
// same placeholders will be all replaced 
// with the same argument :
// "oops {0}{0}".format("!","?")
// returns "oops !!"
//
String.prototype.format = function () {
   var args = arguments; 
   return this.replace( /\{(\d|\d\d)\}/g, 
   function ( $0 ) {
      var idx = $0.match(/\d+/);
      return args[idx] ? args[idx] : $0 ;
   } );
}

"T123{0}T{0}".format("A","B")
/*
T123ATA
*/

Related

  1. format()
    String.prototype.format = String.prototype.f = function(){
      var args = arguments;
      return this.replace(/\{(\d+)\}/g, function(m,n){
        return args[n] ? args[n] : m;
      });
    };
    
  2. format()
    function extend(obj, src) {
        for (var key in src) {
            if (src.hasOwnProperty(key)) obj[key] = src[key];
        return obj;
    String.prototype.format = function () {
      var i = 0, args = arguments;
      return this.replace(/{}/g, function () {
    ...
    
  3. format()
    String.prototype.format = function() {
        var args = Array.prototype.slice.call(arguments),
            format = this,
            match;
        if (args.length === 1 && typeof args[0] === "object") {
            args = args[0];
        for (var i = 0; match = /{(\d+|\w+)?}/gm.exec(format); i++) {
            var key = match[1];
    ...
    
  4. format()
    String.prototype.format = function () {
        var s = this;
        for (var i = 0; i < arguments.length; i++) {
            var reg = new RegExp("\\{" + i + "\\}", "gm");
            s = s.replace(reg, arguments[i]);
        return s;
    
  5. format()
    String.prototype.format = String.prototype.f = function() {
        var s = this,
            i = arguments.length;
        while (i--) {
            s = s.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i]);
        return s;
    };
    function selectText() {
    ...
    
  6. format()
    String.prototype.format = String.prototype.f = function() {
        var s = this,
            i = arguments.length;
        while (i--) {
            s = s.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i]);
        return s;
    };
    function selectElementText(el) {
    ...
    
  7. format()
    String.prototype.format = function() {
      var args = arguments;
      return this.replace(/\{\{|\}\}|\{(\d+)\}/g, function(m, n) {
        if (m == "{{") {
          return "{";
        if (m == "}}") {
          return "}";
        return args[n];
      });
    };
    String.prototype.html = function() {
      return $('<i></i>').text(this).html();
    };
    String.prototype.htmlWithLines = function() {
      var text = this.html();
      return text.replace(/\n/g, '<br/>');
    function loc () {
      var code = arguments[0];
      var text = l[code];
      if (text != null) {
        var params = []  ;
        for (var i = 1 ; i < arguments.length ; i++) {
          params.push(arguments[i]);
        return text.format (params);
      } else {
        return "##" + code + "##";
    
  8. format()
    function generateUUID(){
        var d = new Date().getTime();
        if(window.performance && typeof window.performance.now === "function"){
            d += performance.now(); 
        var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
            var r = (d + Math.random()*16)%16 | 0;
            d = Math.floor(d/16);
            return (c=='x' ? r : (r&0x3|0x8)).toString(16);
    ...
    
  9. format()
    String.prototype.format = function() {
        var formatted = this;
        for (var i = 0; i < arguments.length; i++) {
            var regexp = new RegExp('\\{'+i+'\\}', 'gi');
            formatted = formatted.replace(regexp, arguments[i]);
        return formatted;
    };
    function lz(n){
    ...