Return filtered JSON variable in Jquery : I have this function to return `iconurl` value, according to `compname` value:

    function icons(x){
    	var comand = x.toLowerCase();
    
    	var icon= { "ico":[
    		  {
    			    "iconname": "X",
    			    "iconurl": "resources/img/x.jpg"
    			  },
    			  {
    			    "iconname": "Y",
    			    "iconurl": "resources/img/y.png"
    			  },
    			  {
    			    "iconname": "Z",
    			    "iconurl": "resources/img/z.jpg"
    			  }]
    	};
    
    		var returnedData = $.grep(icon.ico, function (element, index) {
    		    return element.iconname.toLowerCase() == comand;
    		});
    
    		return returnedData[0].iconurl;
        
    }

This works when I call this function only.

When I try to call this function for my variable, I get "Uncaught TypeError: Cannot read property 'iconurl' of undefined".

    var compname = cp/X/;
    var regExp = /[^/cp]+/;
	var regcomp = regExp.exec(compname);
	var icourl = icons(regcomp.toString());

Anyone here where is the problem?

Thanks!