Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
在像 a=b 这样的字符串中,我需要用 == (a==b) 替换等号。但是我不想替换 a<=b 或 a>=b 中的等号。
我看到正则表达式有“(?!expr)后面没有expr。” 但不是“前面没有 expr”。我在 JavaScript 中这样做。
不幸的是,JavaScript 不支持使这项工作轻松进行所需的后视。但是,您可以模拟它们:
.replace(/([^<>])=([^=])/g,"$1==$2")
请注意,这不会替换=字符串开头或结尾的任何内容,但鉴于上下文,我认为这不是问题。
=
你想要负后视。
(?<!a)b
http://www.regular-expressions.info/lookaround.html