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

前端 未结 13 2078
轻奢々
轻奢々 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:37

    This allows you to do exactly that

    NPM: https://www.npmjs.com/package/stringinject

    GitHub: https://github.com/tjcafferkey/stringinject

    By doing the following:

    var str = stringInject("My username is {username} on {platform}", { username: "tjcafferkey", platform: "GitHub" });
    
    // My username is tjcafferkey on Git
    

提交回复
热议问题