gpt4 book ai didi

javascript - 从字符串中提取子字符串并转换为node js中的json对象

转载 作者:行者123 更新时间:2023-12-03 04:37:23 26 4
gpt4 key购买 nike

我有一个像

这样的字符串

This {manish } {abc} {123 } is my string CopyResult={ Region : us-west-2, AmiId : ami-0f60f66f},{ Region : us-west-1, AmiId : ami-2884de48}, manish kumar

我想获取像

这样的子字符串
{ Region : us-west-2, AmiId : ami-0f60f66f},{ Region : us-west-1, AmiId : ami-2884de48}

得到这个信息后,我想将该字符串转换为 JSON。

我正在尝试类似的代码

 var pt = /CopyResult=.*/;
var copyAmisResult = copyAmilog.match(pt);
console.log("copyAmilog -- " + copyAmisResult);

输出

CopyResult={ Region : us-west-2, AmiId : ami-0f60f66f},{ Region : us-west-1, AmiId : ami-2884de48},

有人可以建议我修改后的正则表达式,以便我可以获得输出。

PS:- 我需要从字符串的开头删除 CopyResult= ,并从字符串的最后删除 , ,而且它还可以有多个 {}。之后,有一种更简单的方法将该字符串转换为 JSON 对象。 JSON.parse() 有用吗?

最佳答案

你可以使用这个正则表达式来获取你想要的子字符串,但我不知道如何将它转换为对象

var str = "This {manish } {abc} {123 } is my stringCopyResult={ Region : us-west-2, AmiId : ami-0f60f66f},{ Region : us-west-1, AmiId : ami-2884de48}, manish kumar";
console.log( str.match(/=\s*({.*})/)[1]);

关于javascript - 从字符串中提取子字符串并转换为node js中的json对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43236385/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com