正则表达式-需要帮助获取字符串的特定部分
Regular Expression - Need help on getting specific part of a string
这是标准字符串结构:
{"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"
我真的不擅长正则表达式,如果有任何帮助,我将不胜感激!
提前感谢
数据显示为JSON格式(减去HTML转义)。因此,不需要正则表达式。
相反,直接访问:
var data = {"actor":"100003221104984","target_fbid":"286108458103936", ...}
alert(data.target_profile_id);
看看小提琴。
更新
正如Jonathan所指出的,如果字符串确实包含HTML实体,则需要首先对其进行解析,以创建一个对象,将其分配给上面的示例中的data
。
SO上还有其他帖子回答了如何做到这一点。例如:如何使用jQuery解码HTML实体?
如果您拥有所有这些&现状;你也可以通过获得正确的字符来完成,而不需要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));
尝试:
/"actor":"('[^&']+)/
或
/"actor":"([^&]+)/
我已经理解了你的问题。整个字符串实际上是一个JSON对象,其中引号(")以&.的形式出现
首先替换&引用然后计算表达式,得到你想要的任何项目的值。
以下工作代码:)
<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"
}
}
相关文章:
- 将敲除绑定应用于模板,并将结果作为字符串获取
- 我们可以将ng绑定值存储到php字符串中吗
- 挖空 HTML 绑定和字符串格式的 HTML 长度
- 是否可以使用Angular2将自定义字符串轻松绑定到html元素中的id和for
- Popover未将字符串绑定为html angularjs
- angular js$scope变量与字符串绑定
- angular2中具有属性绑定的字符串串联
- 挖空 JS html 绑定返回奇怪的代码而不是 html 字符串
- 从 jQuery 中的字符串中获取部分值
- 从链接内的数据绑定中获取字符串
- 使用字符串键绑定嵌套对象
- 如何在 angularjs 中绑定为字符串的 html 部分
- 敲除.js如何从绑定构建字符串
- 分析AngularJS中包含数据绑定的字符串
- AngularJS指令:用单向绑定从字符串解析数组
- 为什么不能从ui-bootstrap模态控制器中绑定$scope字符串值?
- Angular 2:使用属性绑定附加字符串
- js最好的方式绑定HTML字符串在文本区与换行符
- foreach绑定到字符串列表时,如何访问字符串
- 将函数绑定到String.原型,所以它总是绑定到字符串