如何使RegEx分割字符串,如
目前我正在使用这个输入= input.match(/ \ w + |" [^"] *" + |([[^]] +))+ / g);
input1:(" java")AND("操作系统" AND" c" AND c ++)
但是输出是:" java",AND,("操作系统" AND" c" AND c ++)
它应该是:(" java"),AND,("操作系统",AND," c",c ++)
答案 0 :(得分:0)
为什么不做一个简单的替换?
string input1 = ("java") AND ("operating systems" AND "c" AND c++);
input1 = input1 .replace("AND ", ",AND,");
答案 1 :(得分:0)
要为您发布的输入提供准确的输出,用逗号替换所有空格,除了引号内的空格:
var originalString = '"("java") AND ("operating systems" AND "c" AND c++)"';
var newString = originalString.replace(/\s+(?=([^"]*"[^"]*")*[^"]*$)/gi, ',');
@Christoffer的建议也会有一点调整,因为你的用例只涉及三个单词(这也让你更容易理解发生了什么):
var originalString = '"("java") AND ("operating systems" AND "c" AND c++)"';
var newString = originalString.replace(/\s+(AND|OR|NOT)\s+/gi, ',$1,');