用空格替换逗号

时间:2018-03-29 09:54:16

标签: javascript regex

我的输入:(((text(text here))) AND (test3) Near (test4) NOT (test5) NOT (Test6)),((tttt,tttt)),((and,lol)),((hbhbhbhbhbh))

我的输出:(((text(text here))) AND (test3) Near (test4) NOT (test5) NOT (Test6) (tttt,tttt) (and,lol) (hbhbhbhbhbh))

我期望的结果:(((text(text here))) AND (test3) Near (test4) NOT (test5) NOT (Test6) ((tttt,tttt)) ((and,lol)) ((hbhbhbhbhbh))

当我有这个字符串),(

时,我想用空格替换逗号

DEMO:



 var txt="(((text(text here))) AND (test3) Near (test4) NOT (test5) NOT (Test6)),((tttt,tttt)),((and,lol)),((hbhbhbhbhbh))"
  var finalResult=txt.replace(/[)],[(]/g," ");

console.log("Result:",finalResult);




1 个答案:

答案 0 :(得分:4)

您应该在结果中加入)(

var txt = "(((text(text here))) AND (test3) Near (test4) NOT (test5) NOT (Test6)),((tttt,tttt)),((and,lol)),((hbhbhbhbhbh))"
var finalResult = txt.replace(/[)],[(]/g, ") (");

console.log("Result:", finalResult);

我得到以下输出:

Result: (((text(text here))) AND (test3) Near (test4) NOT (test5) NOT (Test6)) ((tttt,tttt)) ((and,lol)) ((hbhbhbhbhbh))

另请注意,此处不需要.toString()函数,因为原始txt是一种字符串。