Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/379.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.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 如何在其他字符串中找到Json数据?_Javascript_Json_Regex - Fatal编程技术网

Javascript 如何在其他字符串中找到Json数据?

Javascript 如何在其他字符串中找到Json数据?,javascript,json,regex,Javascript,Json,Regex,我需要使用JavaScript分析字符串,以确定其中是否包含一些JSON 我有一个在PHP上运行的模式: $pattern = ' / \{ # { character (?: # non-capturing group [^{}] # anything that is not a { or } | # OR (?R) # recurses the entire pattern

我需要使用JavaScript分析字符串,以确定其中是否包含一些JSON

我有一个在PHP上运行的模式:

$pattern = '
/
\{              # { character
(?:         # non-capturing group
        [^{}]   # anything that is not a { or }
        |       # OR
        (?R)    # recurses the entire pattern
    )*          # previous group zero or more times
\}              # } character
/x
';

preg_match_all($pattern, $text, $matches);
print_r($matches[0]);
然而,我正在努力将其转换为JavaScript。以下是我到目前为止的情况:

 var txt = draft.content. // gets a long text string
 jsontest = txt.match(/\{(?:[^{}]|(?R))*\}/xg);
但这会得到一个错误,它说:

在(?之后的正则表达式中存在无效字符

有人能建议修改这个正则表达式模式吗?或者在JavaScript中有没有其他不需要正则表达式的方法

以下是输入:

 New test nested braces

 {
 "employee":{ "name":"John", "age":30, "city":"New York" },
 "location":{"street":"100 Main","City":"Houston","Zip":"77001"}
 }

 And other text that is not part of the string I need 
输出应该是json字符串

Ok,以面值表示您的“输入”:

New test nested braces

 {
 "employee":{ "name":"John", "age":30, "city":"New York" },
 "location":{"street":"100 Main","City":"Houston","Zip":"77001"}
 }

 And other text that is not part of the string I need 
这个正则表达式将与您想要的匹配

/{[{}\w\":\s,]*}/g

我找到了另一个解决方案或难题

 {(?<={).*(?=})}

{(?我可以看出我提供的示例非常基本,但是@Liam的答案非常适合,并教会了我如何开始

我发现了一个更健壮的JSON示例,包括括号中的数组、电子邮件地址等,因此我将他的解决方案扩展到:

 /{[{}\[\]\w\!@#\$%\^\&*\)\(+=._":@\?\-\s,]*}/g
我寻找更多的特殊角色。我意识到这是不完整的,所以请注意


好吧,这里是新手的耻辱。我最初的问题是过度劳累和思考

提供的答案(我没有提供适当的上下文)是正确的

但这更简单,特别是如果您确信类似JSON的字符串是正确生成的或可以正确验证的话

 {.*}
请参见此处示例中的测试字符串


效果很好。非常感谢。我的示例很简单,但可以说明JSON字符串可能与其他文本一起出现。其他文本具有实用性,但我的iOS应用程序将我限制为一个输入。我对答案投了赞成票,但我没有足够的分数来推动公共指针。不确定@user8960345 perha是什么意思ps阅读本文,我查看了箭头。你可以看到我是多么的新手。感谢你将我指向链接。评论不用于扩展讨论;此对话已完成。