Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在javascript中从字符串中提取对象_Javascript_Regex_String - Fatal编程技术网

在javascript中从字符串中提取对象

在javascript中从字符串中提取对象,javascript,regex,string,Javascript,Regex,String,我将此文本作为javascript中的字符串: 'ConversationMessage.edit({"conversation_message_id":1901,"conversation_id":154,"resource_id":112,"message":"Great :)","sent_datetime":"2017-05-22T10:04:16.583Z","message_type":0,"display_name":"Hammad Rasheed","resource_image

我将此文本作为javascript中的字符串:

'ConversationMessage.edit({"conversation_message_id":1901,"conversation_id":154,"resource_id":112,"message":"Great :)","sent_datetime":"2017-05-22T10:04:16.583Z","message_type":0,"display_name":"Hammad Rasheed","resource_image":"http://whuntulocal.s3.amazonaws.com/1492584389-Shot.png","messageattachment_id":null,"message_id":null,"directory_name":null,"filename":null})'
我想从中提取对象。这意味着我想提取传递给
ConversationMessage.edit()的内容作为参数

我尝试了
JSON.parse()
,但失败了。 我尝试了
str.split()
to split”)“和”(“character,但这也不起作用,因为对象中的message属性也可以包含“)”字符串,因为它当前包含“Great:)”

以下是提取此字符串的HTML源:

<span class="message-date text-navy"><i class="fa fa-pencil text-success cursor-pointer message-edit-pencil" onclick="ConversationMessage.edit({&quot;conversation_message_id&quot;:1901,&quot;conversation_id&quot;:154,&quot;resource_id&quot;:112,&quot;message&quot;:&quot;Great {{{{{{{&quot;,&quot;sent_datetime&quot;:&quot;2017-05-22T10:04:16.583Z&quot;,&quot;message_type&quot;:0,&quot;display_name&quot;:&quot;Hammad Rasheed&quot;,&quot;resource_image&quot;:&quot;http://whuntulocal.s3.amazonaws.com/1492584389-Shot.png&quot;,&quot;messageattachment_id&quot;:null,&quot;message_id&quot;:null,&quot;directory_name&quot;:null,&quot;filename&quot;:null})"></i> Monday, 22 May 2017, 15:04 </span>
2017年5月22日星期一15:04

我不是javascript方面的专家,有人可能会为您提供更好的javascript解决方案,但对于您当前的字符串,请在正则表达式下方尝试,您将在第一组中获得结果

ConversationMessage\.edit\((\{.*\})\)

简单正则表达式应该可以工作:

var str='ConversationMessage.edit({“conversation_message_id”:1901,“conversation_id”:154,“resource_id”:112,“message:”Great:),“sent_datetime:”2017-05-22T10:04:16.583Z,“message_type”:0,“display_name:”Hammad Rasheed,“resource_image:”http://whuntulocal.s3.amazonaws.com/1492584389-Shot.png“,”消息附件id“:null,“消息id“:null,“目录名称:null,“文件名:null}”);
var objString=/^ConversationMessage\.edit\(.*)$/.exec(str);
var obj=JSON.parse(objString[1]);
console.log(obj);

.as console wrapper{max height:100%!important;top:0;}
您可以获取字符串的子字符串,从第一个
{
字符开始,到
}
字符结束,再加上一个,即

var a='ConversationMessage.edit({“conversation\u message\u id”:1901,“conversation\u id”:154,“resource\u id”:112,“message:“Great:)”,“sent\u datetime:“2017-05-22T10:04:16.583Z”,“message\u type”:0,“display\u name:“Hammad Rasheed”,“resource\u image:”http://whuntulocal.s3.amazonaws.com/1492584389-Shot.png,“消息附件id”:null,“消息id”:null目录_name:null,“filename:null}”

console.log(JSON.parse(a.substring(a.indexOf('{'),a.lastIndexOf('}')+1));
您能提供更多字符串示例吗,即它们是否都以
'ConversationMessage.edit开头(
难道您没有权限编辑conversationMessage@Downvoters向下投票的原因?我采用元素的onclick属性。如上图所示。数据在该格式中是可变的,但格式将始终相同,以修复JSON对象的源,在客户端使用regex清除无效的JSON对象这不是个好主意logic@MinaGabriel也许这是从另一个网站刮来的,他们没有权限更改它?想解释下一票吗?你甚至提到了regex的遥远可能性,下一票会飞起来。这是一个使用regex的完全合法的地方…如果对象包含“{”或“}”'在对象字符串中。如果消息包含
Great:}
而不是
Great:)
,则此操作将失败。我仍然是正则表达式选项的粉丝,即使讨厌正则表达式的人会讨厌正则表达式。Thankyou@FrankerZ我已将indexOf更改为应该有助于解决此问题的最后一个索引:)我不知道还有什么好用的测试用例。但是我不确定如果消息包含多个{或}的话,它是否会一直起作用。会吗?我不确定,如果你提供一些更好的输入示例,我可能能够提供一个更健壮的解决方案