Javascript Regex to match value of JSON key value pair

蹲街弑〆低调 提交于 2021-01-21 08:49:12

问题


Given the following key value pairs, how could I match just the values (including the quotes)?

Explanation: I'm doing a find and replace in my IDE. I have hundreds of key/value pairs where the values need to be changed from strings to objects. So basically replacing the value.

"ElevationFilenameIn": "Input raster elevation file",
"TargetCRS": "Target vertical coordinate reference system Type",
"featureName": "The name of the feature to extract, for example \"Vegetation\" or \"Water\"",
"TargetCRScode": "Target vertical coordinate system Code",
"TargetCRSfile": "The projection (.prj) file in shoebox to be used for this inputfile"

My attempt (which is not working, not even close):

[:]\s*(\"\w*\")

回答1:


You can use the pattern:

[:]\s(\".*\")

and test it following this link: https://regex101.com/r/nE5eV3/1




回答2:


I guess this one does the job also well. One good part it doesn't use any capture groups one bad part it's more costly compared to the accepted answer.

[^:]+(?=,|$)

Regular expression visualization

Debuggex Demo

Regex101 Demo




回答3:


Get value

[^:"]+(?="})

Get value by key

If you wish to select a specific key that can be done like so:

[^:KEY"]+(?="})



来源:https://stackoverflow.com/questions/37007112/javascript-regex-to-match-value-of-json-key-value-pair

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!