结束here我被要求用我的评论之一形成一个新问题,所以我来了。我想知道是否可以只替换某些单词中的短语。例如:替换 CBABAC
中的 BAB
但不替换 DABABCC
中的 BAB
谢谢!
最佳答案
使用 lookahead :
BAB(?=AC)
解释
"BAB" + // Match the characters “BAB” literally
"(?=" + // Assert that the regex below can be matched, starting at this position (positive lookahead)
"AC" + // Match the characters “AC” literally
")"
或
BAB(?!CC)
解释
"BAB" + // Match the characters “BAB” literally
"(?!" + // Assert that it is impossible to match the regex below starting at this position (negative lookahead)
"CC" + // Match the characters “CC” literally
")"
关于Javascript:仅替换某些短语中的字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11692800/