Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Regex to match value of JSON key value pair

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*\")
like image 813
bflemi3 Avatar asked May 03 '16 14:05

bflemi3


People also ask

Is JSON key-value pair?

A JSON object contains zero, one, or more key-value pairs, also called properties. The object is surrounded by curly braces {} . Every key-value pair is separated by a comma.

Can JSON contain regex?

JSON strings can't have functions or regex literals.

Is JSON name value pairs?

JSON Data - A Name and a ValueJSON data is written as name/value pairs (aka key/value pairs).


2 Answers

You can use the pattern:

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

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

like image 61
demartis Avatar answered Nov 02 '22 06:11

demartis


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

like image 40
Redu Avatar answered Nov 02 '22 07:11

Redu