admin管理员组文章数量:1403480
I have a JSON string and need to find an nested object and only want the object matching my regexp returned.
{
"some": {
"nested": {
"stuff": [
{
"bla": "blub1",
"bar": "bar"
},
{
"bla": "blub2",
"bar": "foo"
},
{
"bla": "blub3",
"bar": "foobar"
}
]
}
}
I almost got it with this reg exp /{(.*?"bar": "foo".*?)}/gs
but this does not return only the matching object.
I only want it to return this:
{
"bla": "blub2",
"bar": "foo"
}
See:
I actually don't want to use regex but I'm trying to find the best solution to find a object in a nested object and thought it might work good with regex.
EDIT: I unterstand that this is not a good approach but I just want to try and see the performance between regex and parsing the JSON and and make a deep search
I have a JSON string and need to find an nested object and only want the object matching my regexp returned.
{
"some": {
"nested": {
"stuff": [
{
"bla": "blub1",
"bar": "bar"
},
{
"bla": "blub2",
"bar": "foo"
},
{
"bla": "blub3",
"bar": "foobar"
}
]
}
}
I almost got it with this reg exp /{(.*?"bar": "foo".*?)}/gs
but this does not return only the matching object.
I only want it to return this:
{
"bla": "blub2",
"bar": "foo"
}
See: https://regex101./r/mK3oI6/3
I actually don't want to use regex but I'm trying to find the best solution to find a object in a nested object and thought it might work good with regex.
EDIT: I unterstand that this is not a good approach but I just want to try and see the performance between regex and parsing the JSON and and make a deep search
Share Improve this question edited Aug 24, 2016 at 14:33 Heiko asked Aug 24, 2016 at 11:19 HeikoHeiko 3041 gold badge2 silver badges10 bronze badges 4-
1
regex really isn't the tool to work with json. For instance, a naive implementation will break when JSON string values contain the
}
character. More robust implementations are possible but can't handle every possible case, and are generally not worth the hassle. Maybe you could tell us what environment you're in so that we suggest another more appropriate tool? – Aaron Commented Aug 24, 2016 at 11:21 - 1 Don't use regex for scenarios like this. Just parse your json string to a json object and traves through the object to get the part you want. You can write a match function to it. – Jerad Rutnam Commented Aug 24, 2016 at 11:33
- What exactly are you trying to match? – user663031 Commented Aug 24, 2016 at 13:54
-
As described: the nested object which has the property
"bar"
with the value"foo"
– Heiko Commented Aug 24, 2016 at 14:22
1 Answer
Reset to default 4If you are deadset on using regex:
[^{]*?({\s+"bla[^}]*?})
basically, does a non greedy search until the first { <space> "bar
, captures until the first closing curly brace.
edit: if you want only the Nth instance, [^{]*?(?:{\s+"bla[^}]*?}){N}[^{]*?({\s+"bla[^}]*?})
if you want only the one with bar : foo:
.*{([^}]*bar.*?foo"[^}]*?)}
USING JSON WITH REGEX IS A BAD IDEA. bees fickle and could break easily. It would be a better idea to use one of the primitive iterable types, load the json using the json library, and simply index your way through it to get the data you want.
本文标签: javascriptFind object in JSON with RegExpStack Overflow
版权声明:本文标题:javascript - Find object in JSON with RegExp - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744389745a2603921.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论