0

I have a string like:

const stringVar = ":20:9077f1722efa3632 :12:700 :77E:  :27A:2/2 :21A:9077f1722efa3632 :27:1/2 :40A:IRREVOCABLE"

I want to create JSON from above stringVar:

{
  ":21:" : "9077f1722efa3632",
  ":12:" :  "700",
  ":27A:":  "2/2",
  ":21A:":  "9077f1722efa3632",
  ":27:" :  "1/2",
  ":40A:":  "IRREVOCABLE"
}

So, I was thinking I could split with regular expression (":(any Of char/digit):") I would make the first part the key and the second part its value.

2 Answers 2

2

The regular expression /(:\w+:)(\S+)/ matches the whole key:value pair. You can add the g modifier, and then use it in a loop to get all the matches and put them into the object.

const stringVar = ":20:9077f1722efa3632 :12:700 :77E:  :27A:2/2 :21A:9077f1722efa3632 :27:1/2 :40A:IRREVOCABLE"

var regexp = /(:\w+:)(\S+)/g;
var obj = {};
var match;
while (match = regexp.exec(stringVar)) {
  obj[match[1]] = match[2];
}
console.log(obj);

If you want to create an array of {key: ":20:", value: "9077f1722efa3632"}, you can modify the code to:

const stringVar = ":20:9077f1722efa3632 :12:700 :77E:  :27A:2/2 :21A:9077f1722efa3632 :27:1/2 :40A:IRREVOCABLE"

var regexp = /(:\w+:)(\S+)/g;
var array = [];
var match;
while (match = regexp.exec(stringVar)) {
  array.push({key: match[1], value: match[2]});
}
console.log(array);

If the values can contain space, change the regexp to:

/(:\w+:)([^:]+)\s/g

This will match anything not containing : as the value, but not include the last space.

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

3 Comments

Hey Barmar Thanks for quick respone I have used your solution but in results there are few tags missing can you try this string ":20:9077f1722efa3632 :12:700 :77E: :27A:2/2 :21A:9077f1722efa3632 :27:1/2 :40A:IRREVOCABLE :20:NONREF :40E:EUCP LATEST VERSION :31D:171231 sddfsdf :51A:sdf :50:asdfasdf asdf :59:asdf asdfasdf :32B:CAD23,12 :41A:ADVISING BANK BY DEF PAYMENT :42P:100% Payment At 30 Days :44C:171231 :45A:dfsdf +CFR :46A:+Logistics +Commercial Invoice :47A:9077f172-18ac4c89-8131-fce442efa363 :49:CONFIRM :57A:02DFDFN" and get all tags also :77E: with empty value
i see that some fields are same and json replace previous key if key is same so may be we can return array with [ {"name":"","value":""},{"name":"","value":""}]
Does that have spaces in the values? From your example I thought that space was the delimiter between each :key:value pair.
0

You can achieve the same result without using regex.

const stringVar = ":20:9077f1722efa3632 :12:700 :77E:xxx :27A:2/2 :21A:9077f1722efa3632 :27:1/2 :40A:IRREVOCABLE";

const result = stringVar
  .split(' ')
  .reduce((ret, current) => {
    const pos = current.indexOf(':', 1);
    ret[current.substring(0, pos + 1)] = current.substring(pos + 1);
    return ret;
  }, {});

console.log(result);

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.