0

I need some help to build an array from a shortcode. The problem is the match regex part.

my short code looks like this:

 [email_image,id=1,c=member,fS=15,lH=20]

desired output:

{name: 'email_image', id: '1', c: 'member', fS: '15', lH: '20'}

How to alter my function (regex) to work?

thanks.sepp.

function ajax(ed){

        var str = tinyMCE.activeEditor.getContent();
        var re = /\[email_image([^\]]*)\]/g;
        var found = str.match(re);

        found.map(function(item){
            //console.log(item);  //  [email_image,id=1,c=member,fS=15,lH=20] ....

            arraytophp = {name: 'email_image', id : item.split(',')[1].split('=')[1], c: item.split(',')[2].split('=')[1], fS : item.split(',')[3].split('=')[1], lH: item.split(',')[4].split('=')[1].replace(']','')};


            // PROBLEM START *************************************************************

            var arraytophp_test = {};
            item.match(/([\w-]+)=([^,]+)/g).forEach(function(attribute) {
                //console.log(attribute)
                attribute = attribute.match(/([\w-]+)=(.+?)/);
                arraytophp_test[attribute[1]] = attribute[2];
            });
            console.log(arraytophp_test);

            // PROBLEM END **************************************************************

            jQuery.ajax({
                url: "StringImage_Ajax/ajaxImage",
                type: "POST",
                dataType: "json",
                async:false,
                data: {"data" : JSON.stringify(arraytophp)}
                }).done(function(data){
                    string = data.string;
                    tinyMCE.activeEditor.setContent(tinyMCE.activeEditor.getContent().replace(item,string));    
                });
            });
    }
2
  • Replace function(attribute) with function(match_val, attribute1, attribute2) and then use attribute1 instead of attribute[0] and attribute2 instead of attribute[1] Commented Oct 6, 2016 at 10:34
  • its the regex thats not working as expected. gives me {id: "1", c: "M", fS: "1", lH: "2"} Commented Oct 6, 2016 at 10:45

2 Answers 2

2

Instead of RegEx, basic javascript can do this nicely.

Of course RegEx is great, but sometime it seems as it's used to smash open an egg with a sledge hammer.

var item = '[email_image,id=1,c=member,fS=15,lH=20]';

var result = item.slice(1, -1).split(',').
    reduce(function (a,b) {
      var v=b.split('=');if(v.length===1) a.name=v[0];
      else a[v[0]]=v[1];return a}
,{});

console.log(result);

Sign up to request clarification or add additional context in comments.

Comments

0

EDITED:

I have modified your code this way.

var arr = str.slice(1, -1).split(','), arraytophp_test = {};
$.each(arr, function(key, value){
    if(value.indexOf('=') === -1){
       arraytophp_test.name = value;
    }else{
       var k = value.split('=')[0];
       var v = value.split('=')[1];
       arraytophp_test[k] = v;
    }
})

Where "str" is your string...

4 Comments

Thanks. but output looks like this now: {name: "[email_image", id: "1", c: "Member", fS: "15", lH: "20]"} brackets! i'd like to use the match regex. any idea?
console.log(arraytophp_test) VM2642:1 Object {name: "email_image", id: "1", c: "member", fS: "15", lH: "20"}
Are you sure that output not this? If you tried it in console, dont forget to console.log your arraytophp_test in finish...
str is item. item = [email_image,id=1,c=member,fS=15,lH=20]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.