我浏览了各种帖子,但无法找到解决问题的方法。
我有一个查询字符串,其元素看起来像
location IN ('Place A', 'Place B')
有时我需要看起来像
location IN ('Place A', 'Place B') and person IN ('Security', 'HR')
我希望能够取代" ),"在B地点之后用" )和"所以我可以连接我的查询。有时我会有几个'和'陈述所以这必须是(我猜)全球或' / g'切换替换声明。
我试过了
new_filter_string.replace(/[),]/g, ') and ');
但是我找回了一个删除了应该留下的parens和逗号的查询。我只是想替换它的所有实例的非常具体的parens /逗号字符串。
提前感谢您的帮助。
答案 0 :(得分:0)
subject.replace(/\),$/g, ") and ");
正则表达式解释
),$
Match the character “),” literally «\),»
Assert position at the very end of the string «$»
) and
Insert the character string “) and ” literally «) and»
答案 1 :(得分:0)
[),]
将匹配结束括号或逗号。移除它们周围的[]
以匹配序列),
,并转义)
,因为这是正则表达式中的特殊字符:
new_filter_string.replace(/\),/g, ') and ')