Skip to content Skip to sidebar Skip to footer

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 pai

Solution 1:

You can use the pattern:

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

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

Solution 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

Solution 3:

Get value

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

enter image description here

Get value by key

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

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

enter image description here

Solution 4:

All key value par in complex JSON object.

"[a-zA-Z0-9 -]*"(?=:):[a-zA-Z0-9"-ć]*(?<=")|"[a-zA-Z0-9 -]*"(?=:):[a-zA-Z0-9"-ć]*(?=,)|"[a-zA-Z0-9 -]*"(?=:):[a-zA-Z0-9 "-ć]*(?=\w+)

Solution 5:

Here's one that works when the value is another json object:

:\s*["{](.*)["}]\s*[,}]

It does not include the quotes/brackets in the capture group. If you want to include those, the capture group is easily modified:

:\s*(["{].*["}])\s*[,}]

The expressions also handle varying whitespace since json ignores whitespace.

Post a Comment for "Javascript Regex To Match Value Of Json Key Value Pair"