如何用数组的值替换字符串中的问号? [英] How to replace question marks inside a string with the values of an array?

查看:186
本文介绍了如何用数组的值替换字符串中的问号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Given the string 'Hello ?, welcome to ?' and the array ['foo', 'bar'], how do I get the string 'Hello foo, welcome to bar' in a single line of code with JavaScript (possibly with jQuery, Underscore, etc.)?

推荐答案

var s = 'Hello ?, welcome to ?';
var a = ['foo', 'bar'];
var i = 0;
alert(s.replace(/\?/g,function(){return a[i++]}));

这篇关于如何用数组的值替换字符串中的问号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆