mrrsb mrrsb - 1 year ago 116
jQuery Question

Split string with brace to become parameter

If I have a input value "a[123],b[456],c[789]" and I want to return as "a=123&b=456&c789"

I've tried below code but no luck.. Is there a correct way to implement this?

var str = "a[123],b[456],c[789]"
var string = (str).split(/\[|,|\]/);


Answer Source

One option is:

var rep = {'[':'=', ']': '', ',': '&'};
var query = str.replace(/\[|,|\]/g, el => rep[el]);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download