Format a JavaScript string using placeholders and an object of substitutions?

前端 未结 13 2048
轻奢々
轻奢々 2020-12-07 10:56

I have a string with say: My Name is %NAME% and my age is %AGE%.

%XXX% are placeholders. We need to substitute values there from an object.

13条回答
  •  一生所求
    2020-12-07 11:36

    As a quick example:

    var name = 'jack';
    var age = 40;
    console.log('%s is %d yrs old',name,age);
    

    The output is:

    jack is 40 yrs old

提交回复
热议问题