0
/+(?=([^"\\]*(\\.|"([^"\\]*\\.)*[^"\\]*"))*[^"]*$)/g, "&&"

我使用了这个正则表达式,它用 && 替换了 + 但我如何用“&&”替换字符串行“and”

str = "Loop(this and that is "Help and enjoy")";

它应该返回:Loop(this && that is "Help and enjoy")

4

2 回答 2

1

您是否尝试过更换+

/(?:and)(?=([^"\\]*(\\.|"([^"\\]*\\.)*[^"\\]*"))*[^"]*$)/g, "&&"
于 2013-05-29T12:21:01.153 回答
1

一种方法是在之前匹配双引号中的内容:

var corr = {"and": "&&", "+": "&&"};

str.replace(/"[^"]+"|\+|\band\b/g, function(match) {
    return corr[match] || match;
});
于 2013-05-29T12:36:56.637 回答