gpt4 book ai didi

javascript string.replace 不工作

转载 作者:行者123 更新时间:2023-11-30 12:38:31 26 4
gpt4 key购买 nike

我正在尝试动态替换父字符串中的子字符串。我试过 string.replace('old','new') 无济于事。

这是给我带来麻烦的片段。

newbuffer.replace( matches[i], p.shortcodes[x].vala );
console.log( "Replace " + matches[i] + " with " + p.shortcodes[x].vala );

结果是

"Replace [hello] with <span class="strong"><p>Awaiting ajax request...</p></span">"

我是不是漏掉了什么?.replace 是采用“纯文本”还是我需要做一些正则表达式?

最佳答案

replace 返回一个带有替换的新字符串,它不会更改您调用它的字符串。

newbuffer = newbuffer.replace( matches[i], p.shortcodes[x].vala );

Does .replace take "plain text" or will I need to do some regex?

您可以使用字符串作为“搜索”参数或使用正则表达式调用 replace。如果给它一个字符串,它将替换该字符串的 第一次 出现。如果您给它一个正则表达式,它将替换第一个匹配项或所有匹配项,具体取决于您是否在正则表达式中包含 g(“全局”)标志。

关于javascript string.replace 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25303489/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com