mrrsb mrrsb - 5 months ago 15
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(/\[|,|\]/);

alert(string);




Answer

One option is:

var rep = {'[':'=', ']': '', ',': '&'};
var query = str.replace(/\[|,|\]/g, el => rep[el]);
Comments