1

I have a string:

 str =    'View:{
            Name:"View1",
            Image:{
                BackgroundImage:"Image.gif",
                Position: [0, 0],
                Width: 320,
                Height: 480
            },

            Button:{
                BackgroundImage:"Button.gif",
                Transition:"View2",
                Position: [49, 80],
                Width: 216,
                Height: 71
            },

            Button:{
                BackgroundImage:"Button2.gif",
                Position: [65, 217],
                Width: 188,
                Height: 134
            },'

That I use this regex to add '_#' to elements that have ':{' at the end of them

var i = 0;
str = str.replace(/([^:]+):{/g, function(m, p1) { return p1 + "_" + (++i).toString() + ":{"; });

The ouput is

str =    'View_1:{
        Name:"View1",
        Image_2:{
            BackgroundImage:"Image.gif",
            Position: [0, 0],
            Width: 320,
            Height: 480
        },

        Button_3:{
            BackgroundImage:"Button.gif",
            Transition:"View2",
            Position: [49, 80],
            Width: 216,
            Height: 71
        },

        Button_4:{
            BackgroundImage:"Button2.gif",
            Position: [65, 217],
            Width: 188,
            Height: 134
        },'

Then I do a bunch of stuff with it and now I need to strip out the '#' from it. How would I go about removing those '#'

Not cessary but another problem I am having is the the first regex is incrementing starting from 0 and giving each element the next incremented number. I am trying to make it so that each element increments on its type. Like this:

str =    'View_1:{
        Name:"View1",
        Image_1:{
            BackgroundImage:"Image.gif",
            Position: [0, 0],
            Width: 320,
            Height: 480
        },

        Button_1:{
            BackgroundImage:"Button.gif",
            Transition:"View2",
            Position: [49, 80],
            Width: 216,
            Height: 71
        },

        Button_2:{
            BackgroundImage:"Button2.gif",
            Position: [65, 217],
            Width: 188,
            Height: 134
        },'

Any input on what im doing wrong here too?

6
  • you are getting a syntax error arent you? Commented Dec 28, 2012 at 19:03
  • 2
    Why don't you just fix what ever is building this invalid JSON object :) Seems like it should be build an array of objects. Commented Dec 28, 2012 at 19:04
  • Why do you need the numbers to increment independently? Are you worried about running out of numbers? :) Commented Dec 28, 2012 at 19:22
  • believe it or not, this string was never intended to be a json object. Its an instruction script so I cant modify the incoming string. I have to parse it into an object. Make some changes to the string based on a web form and pass it back - minus the numbers. Make sense? Commented Dec 28, 2012 at 19:28
  • @Barmar :). I could be getting strings with 30 or 40 buttons in it and lots of images and layers. Need some kind of organization. Commented Dec 28, 2012 at 19:30

1 Answer 1

1

For the first question, just replace _\d+:{ with :{

For the second, you need a separate counter for each type. Try this:

var i = {};
str = str.replace(/([^:]+):{/g, function(m, p1) {
    i[p1] = (i[p1] || 0)+1;
    return p1 + "_" + i[p1].toString() + ":{";
});
Sign up to request clarification or add additional context in comments.

6 Comments

I am testing out the 1st one now. But the second one just assign '_1' to all of them. Doesnt seem to increment. here is a demo jsfiddle.net/XLMCc the top shows the output as an object and below that shows the string
Sorry if I am misunderstanding but "just replace '_\d+:{' with ':{'. I do not see '_\d+:{' in my code I provided.
Okay, after some debugging, I've found that you need to use /(\S+):{/g in that regex I gave you, otherwise it's matching stuff from the previous line too which is why it gives you 1 on everything. As for the _\d+:{, this will match the numbers that were added, and remove them if you replace with an empty string.
Thanks Kolink. The first one works great. The question I am having is with the second one. Can you provide the whole .replace() for me. I have tried switching it out and its not working.
.replace(/_\d+:{/g,':{')
|

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.