Reputation: 1
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
Upvotes: 0
Views: 97
Reputation: 10685
I have understood your problem. The whole string is actually a JSON object, where quote(") is present in the form of " ;.
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"
}
}
Upvotes: 0
Reputation: 72971
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.
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?
Upvotes: 2
Reputation: 1915
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));
Upvotes: 1