How to replace question marks inside a string with the values of an array?

前端 未结 5 1197
别那么骄傲
别那么骄傲 2020-12-03 19:11

Given the string \'Hello ?, welcome to ?\' and the array [\'foo\', \'bar\'], how do I get the string \'Hello foo, welcome to bar\' in

5条回答
  •  情话喂你
    2020-12-03 19:46

    let str = 'Hello ?, welcome to ?'
    let arr = ['foo', 'bar']
    const fn = Array.prototype.shift.bind(arr)
    let  result = str.replace(/\?/g, fn)
    
    console.log(result);

提交回复
热议问题