【发布时间】:2021-05-20 13:03:00
【问题描述】:
谁能帮助我使用正则表达式来获取包含 1 个或多个代码的文本中的第一个 json 代码?
我正在尝试使用以下代码:{([^']+)} 但这会得到代码中的所有 jsons
我要获取json中第一个代码的所有文件都有一个或多个代码块,我只需要获取第一个,也就是“Item Definition”后面的代码
带有另外 2 个 json 的文本:
ItemDefinition
{ // I'm trying to get the code from here
"itemid": 590532217,
"shortname": "ammo.nailgun.nails",
"displayName": {
"token": "ammo.nailgun.nails",
"english": "Nailgun Nails"
},
"displayDescription": {
"token": "ammo.nailgun.nails.desc",
"english": "Standard nailgun ammunition"
},
"iconSprite": {
"instanceID": 148798
},
"category": 8,
"selectionPanel": 0,
"maxDraggable": 0,
"itemType": 1,
"amountType": 0,
"occupySlots": 0,
"stackable": 64,
"quickDespawn": false,
"rarity": 0,
"spawnAsBlueprint": false,
"inventorySelectSound": {
"instanceID": 115050
},
"inventoryGrabSound": {
"instanceID": 115050
},
"inventoryDropSound": {
"instanceID": 81228
},
"physImpactSoundDef": {
"instanceID": 60196
},
"condition": {
"enabled": false,
"max": 0.0,
"repairable": false,
"maintainMaxCondition": false,
"foundCondition": {
"fractionMin": 1.0,
"fractionMax": 1.0
}
},
"hidden": false,
"flags": 0,
"steamItem": {
"instanceID": 0
},
"Parent": {
"instanceID": 0
},
"worldModelPrefab": {
"guid": "d8c823436ceda0f42a4da54a972807bf"
},
"Traits": 8,
"panel": {
"instanceID": 0
}
} // Until here
ItemModProjectile
{
...other json code
}
【问题讨论】:
-
s.match(/^{$([^]*?)^}$/m)[1] -
谢谢@WiktorStribiżew sz
标签: javascript json regex search text