I have a set of structured data into a string and I need to iterate over it through regular espression in Javascript.
This is an example of the data:
|KEY1|VALUE 1 |KEY2| VALUE 2 |KEY3| OTHER VALUE WITH ANY CHARACTER LIKE SPACES, |PIPES| AND WHAT YOU WANT |KEY4| VALUE4
I need to parse them and create a structured object (or an array) like this:
myObject.KEY1 = "VALUE 1"
myObject.KEY2 = "VALUE 2"
myObject.KEY3 = "OTHER VALUE WITH ANY CHARACTER LIKE SPACES, AND WHAT YOU WANT"
myObject.KEY4 = "VALUE4"
I've got the keys with that simple regex:[|](.+?)[|] but I don't know how to iterate through them and thei respective values.
|in your data then you must have some way to escape it. There is absolutely no way to distinguish that|PIPES|is not a new key, followed by a new value. Even if keys have a very rigid format, I can always put something that looks like a key in the data. e.g.|KEY3| OTHER VALUE WITH ANY CHARACTER LIKE SPACES, \|PIPES\| AND WHAT YOU WANT