I have this string
product_id:WDC WD2500YS-18S revision:6C07 size(GB):232 state: ONLINE
and I need convert to anything else, like json:
{
"product_id": "WDC WD2500YS-18S",
"revision": "6C07",
"size(GB)": "232",
"state": "ONLINE"
}
I tried this /([^:]+)/g
, but is not working because I need that the WD2500YS-18S be togetter with WDC WD2500YS-18S.
You must use the fact that the key doesn't contains spaces and check what follows with a lookahead.
/([^\s:]+):\s*([^:]+?)(?=\s+[^\s:]+:|\s*$)/g
This solution doesn't rely on lookaheads or lookbehinds, and utilizes the browser's built-in json parser to verify that the output is valid. It leverages the fact that the keys do not contain spaces. So ([^ ]+):
will find a key. We just wrap the key and value with quotation marks and let the built-in json parser do the rest.
var input = 'product_id:WDC WD2500YS-18S revision:6C07 size(GB):232 state: ONLINE';
console.log(JSON.parse(
'{' +
input.replace(/(^| )([^ ]+):/g,'","$2":"').substring(2) +
'"}'
));
outputs:
{
product_id: "WDC WD2500YS-18S",
revision: "6C07",
size(GB): "232",
state: " ONLINE"
}
So, I have an <img> tag that has an onclick attribute. The onclick calls a function called analyze(this), with this being the image. The analyze function does some things to the image that ...
So, I have an <img> tag that has an onclick attribute. The onclick calls a function called analyze(this), with this being the image. The analyze function does some things to the image that ...
how to change the color of a line in a HTML page(text in a pre>) Containing A specifique word for example i want to change the color a any line containig "ERROR" and "Erreur" 2014/05/22 02:27:02 - X....
how to change the color of a line in a HTML page(text in a pre>) Containing A specifique word for example i want to change the color a any line containig "ERROR" and "Erreur" 2014/05/22 02:27:02 - X....
I have the following User object: { "_id" : ObjectId("someId"), "name" : "Bob", "password" : "fakePassword", "follower" : [...], "following" : [..] } I need to paginate over the follower ...
I have the following User object: { "_id" : ObjectId("someId"), "name" : "Bob", "password" : "fakePassword", "follower" : [...], "following" : [..] } I need to paginate over the follower ...
I am working through a project for a company I plan on working for and I was given a code challenge to create a choropleth map of Kenya. I have been able to generate the map using GeoJSON and TopoJSON ...
I am working through a project for a company I plan on working for and I was given a code challenge to create a choropleth map of Kenya. I have been able to generate the map using GeoJSON and TopoJSON ...