Warm tip: This article is reproduced from serverfault.com, please click

javascript-如何用数组的值替换字符串中的问号?

(javascript - How to replace question marks inside a string with the values of an array?)

发布于 2012-01-19 17:57:57

给定字符串'Hello ?, welcome to ?'和数组['foo', 'bar'],如何'Hello foo, welcome to bar'使用JavaScript(可能是jQuery,Underscore等)在一行代码中获取字符串

Questioner
MauroPorras
Viewed
0
Sam Greenhalgh 2012-01-20 02:02:26
var s = 'Hello ?, welcome to ?';
var a = ['foo', 'bar'];
var i = 0;
alert(s.replace(/\?/g,function(){return a[i++]}));