0

I have a string like

This {manish } {abc} {123 } is my string CopyResult={ Region : us-west-2, AmiId : ami-0f60f66f},{ Region : us-west-1, AmiId : ami-2884de48}, manish kumar

I want to get the sub-string like

{ Region : us-west-2, AmiId : ami-0f60f66f},{ Region : us-west-1, AmiId : ami-2884de48}

After getting that I want to convert that string to JSON.

I am trying code like

 var pt = /CopyResult=.*/;
 var copyAmisResult = copyAmilog.match(pt);
 console.log("copyAmilog -- " + copyAmisResult);

Output

CopyResult={ Region : us-west-2, AmiId : ami-0f60f66f},{ Region : us-west-1, AmiId : ami-2884de48},

Could some one please suggest me the modified regular expression so that I can get my output.

PS:- I need to remove CopyResult= from begning of the string and , from last of the string and also it can have multiple {}. After that is there an easier way to convert that string to JSON object. Could JSON.parse() be useful?

5
  • 1
    I'm no good with regex but JSON.parse() will be useful once you get the string JSON.parse('[' + '{"one": 1}, {"two": 2}, {"three": 3}' + ']'); just noticed that your variable names and values aren't wrapped in quotes, need to do that too.. Commented Apr 5, 2017 at 16:13
  • 1
    You should tell your server to return proper JSON instead of doing this hack job. Commented Apr 5, 2017 at 16:13
  • @JosephtheDreamer it's very hard to get the JSON result in my case. so I don't have option. Commented Apr 5, 2017 at 16:18
  • why down vote could you please explain so that i can improve the question Commented Apr 5, 2017 at 16:26
  • 1
    It's pretty ugly with the big .replace chain and might be too fragile but here's an example jsfiddle.net/sgL1fmsL (using Ratan's regex -- ty!) Commented Apr 5, 2017 at 18:25

2 Answers 2

1

You can use this regular expression to get the substring you want, but i'm not sure how to convert it to an object

var str = "This {manish } {abc} {123 } is my stringCopyResult={ Region : us-west-2, AmiId : ami-0f60f66f},{ Region : us-west-1, AmiId : ami-2884de48}, manish kumar";
console.log( str.match(/=\s*({.*})/)[1]);

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

3 Comments

what if multiple {} be there ? I have updated my question.
str.match(/stringCopyResult={.*}/)[0].slice("stringCopyResult=".length) you can use this right?
I've updated it to match for the "=" before it, but it will fail if there "={" that you don't want to catch. You should specify exactly how you want to capture to get the right regex.
0

You can get the data as follow.

var copyAmilog = 'This {manish } {abc} {123 } is my stringCopyResult={ Region : us-west-2, AmiId : ami-0f60f66f},{ Region : us-west-1, AmiId : ami-2884de48}, manish kumar'
var pt = /stringCopyResult=(.*),/;
var copyAmisResult = copyAmilog.match(pt);
console.log("copyAmilog -- " , copyAmisResult[1]);

Comments

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.