如何在字符串中搜索两个字符串?

时间:2023-01-25 07:07:41

I'm trying to get "ID":s and "children":, I kind of can now because lots of people from * helped me, thank you, but now I'd like to search for IDs and children in that sequence. For now I can only get "ID":s or "children":s not both, how can I do it, any help is appreciated?

我正试图获得“ID”:s和“孩子们”:我现在可以了,因为很多来自*的人帮助了我,谢谢你,但现在我想按顺序搜索ID和孩子。现在我只能得到“ID”:s或“孩子”:不是两者,我怎么能这样做,任何帮助都表示赞赏?

What I'm getting:

我得到了什么:

1,3,2

What I'd like to get

我想得到什么

1,children,3,2

My code is right below:

我的代码如下:

var data = '[{"name":"node1","id":1,"is_open":true,"children":[{"name":"child2","id":3},{"name":"child1","id":2}]}]';
var arrays = [];
var t1 = data.match(/"id":[0-9]+(,|})*/g);
//arrays = t1;
for (var x = 0; x < t1.length; x++) {
  arrays.push(t1[x].match(/\d+/));

}
//var t2=data.match(/"children":/g);
alert(arrays /*+"\n\n\n\n\n"+t2*/ );

"Live long and prosper"

“健康长寿·繁荣昌盛”

Thanks in advance.

提前致谢。

1 个解决方案

#1


2  

The best way is parsing your string to a JSON: http://api.jquery.com/jquery.parsejson/

最好的方法是将字符串解析为JSON:http://api.jquery.com/jquery.parsejson/

var obj = jQuery.parseJSON( '{"name":"node1","id":1,"is_open":true,"children":[{"name":"child2","id":3},{"name":"child1","id":2}]}' );

console.log( obj.id );
console.log( obj.children[0].id );
console.log( obj.children[1].id );

#1


2  

The best way is parsing your string to a JSON: http://api.jquery.com/jquery.parsejson/

最好的方法是将字符串解析为JSON:http://api.jquery.com/jquery.parsejson/

var obj = jQuery.parseJSON( '{"name":"node1","id":1,"is_open":true,"children":[{"name":"child2","id":3},{"name":"child1","id":2}]}' );

console.log( obj.id );
console.log( obj.children[0].id );
console.log( obj.children[1].id );