Question

So I have a client who does not allow any server side coding, except in rare occurences classic asp, so everything is HTML and javascript.

So basically I need to build a URL from the form and then redirect. Javascript isn't necessarily my thing, but this would take me 5 minutes in asp.net using String.Format.

Is there a String.Format method in javascript?

Was it helpful?

Solution

Ouch, that sucks.

Stolen from another post:

String.format = function() {
  var s = arguments[0];
  for (var i = 0; i < arguments.length - 1; i++) {       
    var reg = new RegExp("\\{" + i + "\\}", "gm");             
    s = s.replace(reg, arguments[i + 1]);
  }

  return s;
}

OTHER TIPS

no, there's no such thing in javascript, but some people have already written a printf for js

e.g JavaScript equivalent to printf/string.format

I was looking for a similar thing and settled on Prototype's "Template" object.

From the Prototype examples

// the template (our formatting expression) var myTemplate = new Template( 'The TV show #{title} was created by #{author}.');

// our data to be formatted by the template var show = { title: 'The Simpsons', author: 'Matt Groening', network: 'FOX' };

// let's format our data myTemplate.evaluate(show); // -> "The TV show The Simpsons was created by Matt Groening."

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top