Paulo Spachi
Paulo Spachi

Reputation: 1

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

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

Answers (4)

Umesh Patil
Umesh Patil

Reputation: 10685

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"
    }
}

Upvotes: 0

Jason McCreary
Jason McCreary

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.

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?

Upvotes: 2

Rogel Garcia
Rogel Garcia

Reputation: 1915

If you have all these &quo te; stuff you could also do it by getting the right chars, without regex

var x = 
"{&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 begin = x.indexOf("target_profile_id")+ "target_profile_id".length + "&quot;:&quot;".length;
var endString = x.substring(begin, x.length);
var end = endString.indexOf("&quot;") + begin;
alert(x.substring(begin, end));

Upvotes: 1

Related Questions