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

发布于 2024-12-27 21:56:49 字数 169 浏览 2 评论 0原文

给定字符串 'Hello ?,welcome to ?' 和数组 ['foo', 'bar'],如何获取字符串 'Hello foo ,欢迎使用 JavaScript(可能使用 jQuery、Underscore 等)在一行代码中使用 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.)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

陈甜 2025-01-03 21:56:49
var s = 'Hello ?, welcome to ?';
var a = ['foo', 'bar'];
var i = 0;
alert(s.replace(/\?/g,function(){return a[i++]}));
var s = 'Hello ?, welcome to ?';
var a = ['foo', 'bar'];
var i = 0;
alert(s.replace(/\?/g,function(){return a[i++]}));
我是男神闪亮亮 2025-01-03 21:56:49

将所有内容都放在一行中有点愚蠢,但是:

var str = 'Hello ?, welcome to ?',
    arr = ['foo', 'bar'],
    i = 0;


while(str.indexOf("?") >= 0) { str = str.replace("?", arr[i++]); }

Kind of silly to put it all on one line, but:

var str = 'Hello ?, welcome to ?',
    arr = ['foo', 'bar'],
    i = 0;


while(str.indexOf("?") >= 0) { str = str.replace("?", arr[i++]); }
一杯敬自由 2025-01-03 21:56:49

您可以使用 vsprintf。尽管如果包含 sprintf,它就不止一行了。

vsprintf('Hello %s, welcome to %s', [foo, bar]);

You could use vsprintf. Although if you include sprintf, it's much more than one line.

vsprintf('Hello %s, welcome to %s', [foo, bar]);
待天淡蓝洁白时 2025-01-03 21:56:49
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);

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);

捂风挽笑 2025-01-03 21:56:49

让我们构建一个 replaceWith 函数:

const replaceWith =
  (...str) =>
    (fallback) =>
      str.length === 0
        ? fallback
        : str.shift();

它是一个柯里化函数:它接受替换字符串列表并返回另一个函数,该函数将逐个返回替换内容:

const replacements = replaceWith('

Let's build a replaceWith function:

const replaceWith =
  (...str) =>
    (fallback) =>
      str.length === 0
        ? fallback
        : str.shift();

It's a curried function: it takes a list of replacement strings and returns another function which will return the replacements one by one:

const replacements = replaceWith('????', '????', '????');
replacements(); //=> '????'
replacements(); //=> '????'
replacements(); //=> '????'

After the third call you have exhausted all the replacements so the next calls will either return undefined or whatever you passed as a fallback parameter:

replacements(); //=> undefined
replacements('????'); //=> '????'

This fallback parameter can be handy if you have more substrings to replace than replacement strings at your disposal. (But we will come to that later.)

Now using replaceWith we can do:

'Hello ?, welcome to ?'.replace(/\?/g, replaceWith('foo', 'bar'));
//=> 'Hello foo, welcome to bar'

Behind the scene it calls the function returned by replaceWith (let's call it fn) with:

fn('?'); //=> 'foo'
fn('?'); //=> 'bar'

The fallback parameter (i.e. your placeholder '?' in this case) is ignored while there are still replacement strings. However it will be returned as is when there are more placeholders than replacements:

'Hello ? and ?, welcome to ?'.replace(/\?/g, replaceWith('foo', 'bar'));
//=> 'Hello foo and bar, welcome to ?'

Acknowledgment

This answer is a variation of Alex Varghese excellent answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文