0

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

4 Answers4

2

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?

Community
  • 1
  • 1
Jason McCreary
  • 71,546
  • 23
  • 135
  • 174
  • If that is stored in a string like the question suggests it will first need to be parsed to safely create an object. See http://www.json.org/js.html – Jonathan Jan 05 '12 at 18:23
  • @Jonathan, indeed. As I noted, the string is ultimately JSON and can be access as shown in my example. I will update with your note for completeness though. Thanks. – Jason McCreary Jan 05 '12 at 18:25
1

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));
Rogel Garcia
  • 1,895
  • 14
  • 16
0

Try:

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

or

/"actor":"([^&]+)/

Ayman Safadi
  • 11,502
  • 1
  • 27
  • 41
0

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="{&quot;actor&quot;:&quot;100003221104984&quot;,&quot;target_fbid&quot;:&quot;286108458103936&quot;,&quot;target_profile_id&quot;:&quot;100003221104984&quot;,&quot;type_id&quot;:&quot;17&quot;,&quot;source&quot;:&quot;1&quot;,&quot;assoc_obj_id&quot;:&quot;&quot;,&quot;source_app_id&quot;:&quot;2305272732&quot;,&quot;extra_story_params&quot;:[],&quot;content_ti&quot;:{&quot;actor&quot;:&quot;100003221104984&quot;,&quot;target_fbid&quot;:&quot;286108458103936&quot;,&quot;target_profile_id&quot;:&quot;100003221104984&quot;,&quot;type_id&quot;:&quot;17&quot;,&quot;source&quot;:&quot;1&quot;,&quot;assoc_obj_id&quot;:&quot;&quot;,&quot;source_app_id&quot;:&quot;2305272732&quot;,&quot;extra_story_params&quot;:[],&quot;content_timestamp&quot;:&quot;1325711938&quot;,&quot;check_hash&quot;:&quot;892251599922cc58&quot;}}";
var ans=str1.split("&quot;").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"
    }
}
Umesh Patil
  • 10,475
  • 16
  • 52
  • 80