3

I'm using javascript and jquery, I have a string like

  var apiUrlAnswer = 'https://api.stackexchange.com/2.0/answers/{ids}?order=desc&sort=activity&site=stackoverflow';

I need replace the {ids} with a variable.

In C# is possible to use a method called

String.Format(https://api.stackexchange.com/2.0/answers/{0}?order=desc&sort=activity&site=stackoverflow, myVariable);

I would like to know if exist something similar in Javascript so I can avoid simple string concatenation.

Thanks for your help!

5 Answers 5

5

Use the String .replace() method:

var apiUrlAnswer = 'https://api.stackexchange.com/2.0/answers/{ids}?order=desc&sort=activity&site=stackoverflow';

apiUrlAnswer = apiUrlAnswer.replace("{ids}", yourVariable);

// OR, if there might be more than one instance of '{ids}' use a regex:
apiUrlAnswer = apiUrlAnswer.replace(/\{ids\}/g, yourVariable);
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks nnnnnn.. do you know alternative day to do it?
There are any number of ways to do it using whatever combination of JavaScript's String manipulation methods takes your fancy, but .replace() is an easy way to do it with one statement...
P.S. There are some libraries that implement a String.format() type function, e.g., sprintf() for JavaScript. If you Google "JavaScript String.format" you'll get a bunch of options.
3

Well here's a String.format (like c#); I've extended javascripts String object to include a format method like this:

String.format = function(stringToFormat, argsForFormat) {

    for (var i = 0; i < argsForFormat.length; i++) {
        var regex = new RegExp('\{(' + i + ')}', 'g');
        stringToFormat = stringToFormat.replace(regex, argsForFormat[i]);
    }

    return stringToFormat;    
};

You can call it like this

var args = [1, 'activity', 'stackoverflow', 'desc']   
var apiUrlAnswer = 'https://api.stackexchange.com/2.0/answers/{0}?order={3}&sort={1}&site={2}';
var apiUrlAnswer = String.format(apiUrlAnswer, args);

Or an inline version would look like this

var apiUrlAnswer = String.format('https://api.stackexchange.com/2.0/answers/{0}?order={3}&sort={1}&site={2}', [1, 'activity', 'stackoverflow', 'desc']);

and another example

String.format("http://{0}.{1}.com", ['www', 'stackoverflow']);

2 Comments

+1. Nice. Or even better, replace(new RegExp('{'+i+'}', 'g'), ... so that it can replace multiple instances of {0}.
@nnnnnn amended as suggested but could only get it to work with a more complex pattern '\{(' + i + ')}'
1

If you are doing this frequently, a more robust alternative to using a regex is to use a client side templating engine like mustache.js. I have used this successfully and it's quick and lightweight.

var view = {
  ids: "Andrew"
};

var output = Mustache.render("https://api.stackexchange.com/2.0/answers/{{ids}}?order=desc&sort=activity&site=stackoverflow", view);

This has the advantage of separating your data and presentation nicely.

Comments

0

How about just using replace:

apiUrlAnswer = apiUrlAnswer.replace('{ids}', 'test');

Comments

0

Try

var id = 'your_id';
var apiUrlAnswer = 'https://api.stackexchange.com/2.0/answers/'+id+'?order=desc&sort=activity&site=stackoverflow';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.