here上,我被要求用我的一个评论提出一个新问题,所以我在这里。我想知道是否有可能仅在某些单词中替换一个短语。例如:替换BAB中的CBABAC而不是BAB中的DABABCC谢谢!

最佳答案

使用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 - Javascript:仅在某些短语中替换字母,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11692800/

10-11 03:10