How do I put variables inside javascript strings?

前端 未结 13 1115
感动是毒
感动是毒 2020-12-12 15:12
s = \'hello %s, how are you doing\' % (my_name)

That\'s how you do it in python. How can you do that in javascript/node.js?

13条回答
  •  攒了一身酷
    2020-12-12 15:55

    const format = (...args) => args.shift().replace(/%([jsd])/g, x => x === '%j' ? JSON.stringify(args.shift()) : args.shift())
    
    const name = 'Csaba'
    const formatted = format('Hi %s, today is %s and your data is %j', name, Date(), {data: {country: 'Hungary', city: 'Budapest'}})
    
    console.log(formatted)

提交回复
热议问题