Regular Expression - Need help on getting specific part of a string - javascript

This is the stantard string structure:
{"actor":"100003221104984","target_fbid":"286108458103936","target_profile_id":"100003221104984","type_id":"17","source":"1","assoc_obj_id":"","source_app_id":"2305272732","extra_story_params":[],"content_timestamp":"1325711938","check_hash":"892251599922cc58"}
the only part i need from this string is the numeric value after the "target_profile_id", in this case, would be "100003221104984"
I really suck at regular expressions and any help would be really appreciated !
Thanks in advance

The data appears to be in JSON format (minus HTML escaped). As such, there is no need for a regular expression.
Instead, access it directly:
var data = {"actor":"100003221104984","target_fbid":"286108458103936", ...}
alert(data.target_profile_id);
See the fiddle.
UPDATE
As noted by Jonathan, if the string indeed includes HTML entities, you will need to first parse it to create an object to assign to data in my example above.
There are additional posts on SO that answer how to do that. For example: How to decode HTML entities using jQuery?

If you have all these &quo te; stuff you could also do it by getting the right chars, without regex
var x =
"{"actor":"100003221104984","target_fbid":"286108458103936","target_profile_id":"100003221104984","type_id":"17","source":"1","assoc_obj_id":"","source_app_id":"2305272732","extra_story_params":[],"content_timestamp":"1325711938","check_hash":"892251599922cc58"}";
var begin = x.indexOf("target_profile_id")+ "target_profile_id".length + "":"".length;
var endString = x.substring(begin, x.length);
var end = endString.indexOf(""") + begin;
alert(x.substring(begin, end));

Try:
/"actor":"(\[^&\]+)/
or
/"actor":"([^&]+)/

I have understood your problem. The whole string is actually a JSON object, where quote(") is present in the form of &quot ;.
First replace & quote; it with ". Then evaluate the expression and get the value of any item you want.
Below working code :)
<script type="text/javaScript">
var str1="{"actor":"100003221104984","target_fbid":"286108458103936","target_profile_id":"100003221104984","type_id":"17","source":"1","assoc_obj_id":"","source_app_id":"2305272732","extra_story_params":[],"content_ti":{"actor":"100003221104984","target_fbid":"286108458103936","target_profile_id":"100003221104984","type_id":"17","source":"1","assoc_obj_id":"","source_app_id":"2305272732","extra_story_params":[],"content_timestamp":"1325711938","check_hash":"892251599922cc58"}}";
var ans=str1.split(""").join("\"");
var obj=eval("(" + ans+ ')');
alert(obj.target_profile_id);
</script>
And see how your actual Data looks like:
{
"actor": "100003221104984",
"target_fbid": "286108458103936",
"target_profile_id": "100003221104984",
"type_id": "17",
"source": "1",
"assoc_obj_id": "",
"source_app_id": "2305272732",
"extra_story_params": [],
"content_ti": {
"actor": "100003221104984",
"target_fbid": "286108458103936",
"target_profile_id": "100003221104984",
"type_id": "17",
"source": "1",
"assoc_obj_id": "",
"source_app_id": "2305272732",
"extra_story_params": [],
"content_timestamp": "1325711938",
"check_hash": "892251599922cc58"
}
}

Related

How do I handle newlines in JSON keys

I have some poorly formatted JSON which has newlines in the keys.
The raw JSON looks like:
{
"Some\\nKey": "Some\\nvalue",
"Some nice key": "Some nice value"
}
I have a matching data source which, for keys without newlines works very happily to convert "Some nice key" into "Some nice value". However, when the matching data source contains newlines, the lookup fails. My code looks something like:
const translations = JSON.parse(fileContents);
var value = translations[key];
if (value == null) {
const fixedKey = JSON.stringify(key).replace(/\\n/g, "\\\\n");
value = translations[fixedKey];
if (value == null) {
console.log(`Translation missing for key: ${fixedKey}`);
}
}
The console output is Translation missing for key: Some\\nKey
So, my question is: In Javascript, how do I look up a value of a JSON key with new lines in the key?
Edit:
So, there were two problems with my code - one external to the post, and one internal. The external problem is that the keys coming from the external data source were somehow malformed when doing lookups with newlines - no idea what, but they were. The second issue was with creating fixedKey - JSON.stringify adds " characters to the start and end, so my console output that I originally put was a lie because I wasn't copy/pasting but recreating by hand - I had missed that the output was actually Translation missing for key: "Some\\nKey". The resulting fix was const fixedKey = JSON.stringify(key).replace(/^"/, "").replace(/"$/,""); - using Stringify and stripping the leading and trailing " characters.
This works for me. Not sure what your issue is
const key = "Some Key";
const jsonString = `{ "Some\\nKey": "Some\\nvalue", "Some nice key": "Some nice value" }`
const obj = JSON.parse(jsonString.replaceAll(/\\n/g," "))
console.log(obj[key])
This also works but shows the value with a newline
const key = "Some\nKey";
const jsonString = `{ "Some\\nKey": "Some\\nvalue", "Some nice key": "Some nice value" }`
const obj = JSON.parse(jsonString)
console.log(obj[key])
If your keys and values are the same, it shouldn't matter that there's a \n in the key or not.
const translations = {
"test\n1": 1,
"test\\n2": 2
};
console.log(translations["test\n1"]);
console.log(translations["test\\n2"]);

Return a javascript object from string using Regex or alternative methods

I have a function in Javascript to match text between curly braces (including the braces) in order to extract a JSON string from different posts. The function is below:
function eventObject(value) {
var json = text.match(/{([^}]+)}/)[0]; // matches content between curly braces
return json;
}
The input looks like this:
{
"host": "Host link..",
"info": "Info text...",
"links": [ {"title": "link_title", "url": "link_url"}, {"title": "link_title_2", "url": "link_url_2"} ],
"category": "campfire"
}
The problem is this text contains a nested string with more curly brackets. The output cuts as soon as it gets to the first occurrence of a closing bracket in the links. How can I prevent this from happening in order to get the full string?
Update
I realised I left out some important information to simplify my question: the API response is a string of raw html that contains the string I would like to parse as an object. The typical raw HTML looks like this:
"cooked":"<pre><code class=\"lang-auto\">{\n\"host\": \"host_link\",\n\"info\": \"event_info\",\n\"links\": [{\"title\": \"link_title \", \"url\": \"link_url"},{\"title\": \"link_two_title \", \"url\": \"link_two_url\"} ],\n\"category\": \"category\"\n}\n</code></pre>"}
The challenge is extracting the entire string between <code></code> and parsing it into an object. I have updated the title of the question to reflect this.
The following function successfully extracts the string and strips the html tags and line breaks, but it does not correctly parse it as an object:
function eventObject(value){
const doc = new DOMParser().parseFromString(value, "text/html");
var json = [...doc.querySelectorAll('code')].map(code => code.textContent); // DOMParser extracts text between <code> tags
var final = String(json).replace(/\n/g, " ").replace(/[\u2018\u2019]/g, "'").replace(/[\u201C\u201D]/g, '"'); // removes line breaks and replaces curly quotes with straight quotes
var string = JSON.stringify(final);
var obj = JSON.parse("'" + string + "'");
return obj;
}
Your function eventObject looks ok, but you don't need JSON.stringify because it is intended for serializing JavaScript objects, whereas you pass a string to it. Try this:
function eventObject(value){
const doc = new DOMParser().parseFromString(value, "text/html");
var json = [...doc.querySelectorAll('code')].map(code => code.textContent); // DOMParser extracts text between <code> tags
var final = String(json).replace(/\n/g, " ").replace(/[\u2018\u2019]/g, "'").replace(/[\u201C\u201D]/g, '"'); // removes line breaks and replaces curly quotes with straight quotes
// var string = JSON.stringify(final);
var obj = JSON.parse(final);
return obj;
}
var value = '"cooked":"<pre><code class=\"lang-auto\">{\n\"host\": \"host_link\",\n\"info\": \"event_info\",\n\"links\": [{\"title\": \"link_title \", \"url\": \"link_url"},{\"title\": \"link_two_title \", \"url\": \"link_two_url\"} ],\n\"category\": \"category\"\n}\n</code></pre>"}';
console.log(eventObject(value))

Get the substring between two substrings in a string [Javascript]

Get 3 or 57 in these examples
I have a list of objects that contain JSON values of these:
For some reason I will skip, the result will be as follows and I want to get the value of the CubicMillimeters by using Regex. How will I do this?
var json1 = "{
"CubicMillimeters": 3,
"Longtitude": 342.152345,
"Latitude": 321.332331
}"
var json2 = "{
"CubicMillimeters": 57,
"Longtitude": 342.152345,
"Latitude": 321.332331
}"
Thanks :D Greatly appreciate your help!
Using JSON.parse seems to be the right approche.
But if you really want (for some reason) the regex solution, here it is:
var json = '{"CubicMillimeters": 3,"Longtitude": 342.152345, "Latitude": 321.332331 }',
reg = /"CubicMillimeters":\s*(.*?),/;
console.log(json.match(reg)[1]);
DEMO with explanation

Parsing JSON where String has Apostrophe Right at Beginning of Property Value - Next to Quotation Mark

I am having trouble parsing a string into a JSON object. The troublesome aspect is two-fold:
there is a phrase surrounded by quotes ; and
that phrase appears at the very beginning of the string - so
immediately after a quote character.
I have some server code which renders into my page the following into an inline script element:
var str = '[{"Id":null,"Name":"\"A gamma\" bla bla rest of string","Code":null,"OtherBits":null,"IsActive":true,}]';
So you can see, the problem is the string value for the "Name" property.
The only thing I have so far which works is as follows, but obviously it is hard-coded and only works with that example:
var str = '[{"Id":null,"Name":"\"A gamma\" bla bla rest of string","Code":null,"OtherBits":null,"IsActive":true,}]';
var escapedString = str.replace('"\"', '"\\"');
var escapedString2 = escapedString.replace('a\"', 'a\\"');
var existingDataForItems = JSON.parse(escapedString2);
I need to generalize it.
Note, you would think it would be as simple as:
var escapedString = str.replace('\"', '\\"');
It is not. That does not work.
As mentioned in my comment, just assign an object / array literal instead of attempting to wrap it in a string and parse it.
From the comments it seems you're using PHP so assuming you have a server-side array $data...
var existingDataForItems = <?= json_encode($data) ?>;
This will result in something like (formatting added for clarity)
var existingDataForItems = [{
"Id": null,
"Name": "\"A gamma\" bla bla rest of string",
"Code": null,
"OtherBits": null,
"IsActive": true
}];

How to regex everything between colon and space?

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"
}
Try /([^:]+):\s?([^:]+)(?:\s|$)/g - regex101

Categories