[英]Regex: Match all double quotes in string and add comma
I need to turn a string like this: 我需要把这样的字符串:
' query: "help me" distance: "25" count: "50" '
into a javascript object or json string that looks like this: 变成如下所示的javascript对象或json字符串:
'{ query: "help me", distance: "25", count: "50" }'
Something like this, perhaps: 大概是这样的:
var query = ' query: "help me" distance: "25" count: "50"';
query = '{' + query.replace(/"(?=\s)/g, '",') + '}';
console.log(query);
With that lookahead expression I just put comma after all the double quotation marks that are followed by whitespace symbol. 使用该超前表达式,我只需在所有双引号后加上逗号,然后再使用空格符号。
Having said that, I'd strongly suggest reconsidering the method of constructing your params: somehow I feel you could get away with simple JSON.stringify
-ing the params. 话虽如此,我强烈建议重新考虑构造参数的方法:以某种方式,我觉得您可以通过简单的JSON.stringify
-ing the params JSON.stringify
。 It will be much more bulletproof - and easier to parse too. 它将更加防弹-并且也更容易解析。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.