您好,我只需要替换一次出现的子字符串 示例:
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/