Replace multiple strings with multiple other strings

前端 未结 18 2006
别那么骄傲
别那么骄傲 2020-11-22 04:14

I\'m trying to replace multiple words in a string with multiple other words. The string is \"I have a cat, a dog, and a goat.\"

However, this does not produce \"I ha

18条回答
  •  别跟我提以往
    2020-11-22 05:07

    I wrote this npm package stringinject https://www.npmjs.com/package/stringinject which allows you to do the following

    var string = stringInject("this is a {0} string for {1}", ["test", "stringInject"]);
    

    which will replace the {0} and {1} with the array items and return the following string

    "this is a test string for stringInject"
    

    or you could replace placeholders with object keys and values like so:

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

提交回复
热议问题