regex to find a string that comes after =

前端 未结 6 1226
萌比男神i
萌比男神i 2021-01-28 07:15

I´m really new to regex and I have been looking around to find an answer but either it dont work or I get some kind of error so I will try to ask the question and hopefulyl some

6条回答
  •  闹比i
    闹比i (楼主)
    2021-01-28 08:07

    function getObject(str) {
    
        var props = str.split(/\[(.*?)\]/g),
            object = {};
        if (props.length) {
            object.name = props.shift();
            while (props.length) {
                var prop = props.shift().split("=");
                if(prop.length == 2){
                    object[prop[0]] = prop[1];
                }
            }
        }
        return object;
    }
    
    console.log(getObject("car[brand=saab][wheels=4]"));
    

提交回复
热议问题