gpt4 book ai didi

javascript - 将第一次出现的字符串替换为另一个字符串

转载 作者:行者123 更新时间:2023-11-28 13:23:13 25 4
gpt4 key购买 nike

尝试在网络中找到它,但没有成功..

假设我有以下字符串:

this is a string test with a lot of string words here another string string there string here string.

我需要将第一个“这里”之后的第一个“string”替换为“anotherString”,因此输出将是:

this is a string test with a lot of string words here another anotherString string there string here string.

谢谢大家的帮助!

最佳答案

仅替换第一次出现的情况时,无需添加 g 修饰符。

str.replace(/\b(here\b.*?)\bstring\b/, "$1anotherString");

DEMO

关于javascript - 将第一次出现的字符串替换为另一个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31423872/

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