Skip to content
Advertisement

Find passwords values in JSON objects using Regex

I have a big JSON object which contains a lot of different JSON, most of them have the structure below (key: sometext-v1.password, and value: password for example:

JavaScript

I want to use Regex to extract all passwords by a name which contains ‘password’ string and its value, but I don’t want to iterate the JSON name by name because this takes a lot of time for processing.

I tried this, but it isn’t working:

JavaScript

Advertisement

Answer

You can use

JavaScript

See the regex demo. Details:

  • "name" – a literal string
  • s*:s* – a : enclosed with optional whitespaces
  • " – a " char
  • [^"]*password[^"]*password enclosed with 0 or more chars other than a "
  • ", – a ", string
  • s* – zero or more whitespaces
  • "value" – a literal text
  • s*:s* – a : enclosed with optional whitespaces
  • " – a " char
  • ([^"]*) – Group 1: any zero or more chars other than ".

See a Java demo:

JavaScript
Advertisement