【问题标题】:replacing a promise string not executed [duplicate]替换未执行的承诺字符串[重复]
【发布时间】:2018-06-17 18:36:23
【问题描述】:

我正在尝试替换 massive string 中的所有“formattedTime”实例。 这是我的代码

googleTrends.interestOverTime({keyword: selectedTopic})
    .then((results) => {
    results.replace(/formattedTime/g, 'no what the heck');
    console.log(results);
})
    .catch((err) => {
    console.error('Oh no there was an error', err);
});

我没有收到任何错误,但是我的控制台内没有发生任何事情。 我什至尝试替换一个单行字符串,但它都不起作用。可能是因为我收到的承诺? 我对 RegEx 比较陌生,所以我认为这可能是一个问题,但似乎不是。

【问题讨论】:

  • replace() 方法在字符串中查找指定值或正则表达式,并返回替换指定值的新字符串;从而更新或返回结果。

标签: javascript regex replace promise


【解决方案1】:

Strig.prototype.replace 不会改变字符串。字符串是不可变的。它将返回一个新字符串。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-26
    • 2018-07-20
    • 2019-05-29
    • 1970-01-01
    • 2018-09-10
    • 2011-10-10
    • 2021-07-06
    • 2021-06-17
    相关资源
    最近更新 更多