问题
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.
[^:]+(?=,|$)

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