gpt4 book ai didi

javascript - 正则表达式 : Match only single substring occurrence

转载 作者:行者123 更新时间:2023-11-30 09:05:40 33 4
gpt4 key购买 nike

您好,我只需要替换一次出现的子字符串示例:

Some sentence #x;#x;#x; with #x;#x; some #x; words #x; need in replacing.

只需要用#y;替换单个#x;并得到后面的字符串

Some sentence #x;#x;#x; with #x;#x; some #y; words #y; need in replacing.

一些注意事项:我的字符串将包含 unicode 字符并且运算符\b 不起作用。

最佳答案

你可以匹配#x;重复任意次数,并且只替换出现一次的那些:

sentence = sentence.replace(/((?:#x;)+)/g, function(m) {
return m.length == 3 ? '#y;' : m;
});

关于javascript - 正则表达式 : Match only single substring occurrence,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5257724/

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