这是标准的字符串结构:
{"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"}
我需要这个字符串的唯一部分是“target_profile_id”之后的数值,在这种情况下,将是“100003221104984”
我真的很喜欢正则表达式,任何帮助都会非常感激!
提前致谢
答案 0 :(得分:2)
数据似乎是JSON格式(减去HTML转义)。因此,不需要正则表达式。
而是直接访问它:
var data = {"actor":"100003221104984","target_fbid":"286108458103936", ...}
alert(data.target_profile_id);
请参阅the fiddle。
如 Jonathan 所述,如果字符串确实包含HTML实体,则需要首先解析它以创建一个对象,以便在上面的示例中分配给data
。
SO上还有其他帖子可以解答如何做到这一点。例如:How to decode HTML entities using jQuery?
答案 1 :(得分:1)
如果你拥有所有这些& quo te;你可以通过获得正确的字符,没有正则表达式来做到这一点
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));
答案 2 :(得分:0)
答案 3 :(得分:0)
我理解你的问题。整个字符串实际上是一个JSON对象,其中quote(“)以& quot;的形式出现。
首先替换&引用;用“。然后评估表达式并得到你想要的任何项目的值。
以下工作代码:)
<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>
看看您的实际数据如何:
{
"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"
}
}