0

I'm curently working on a college project. I'm trying to create a regex expression to return the values from a JSON. Example:

JSON expression: [{"id": "120","name": "Mary","gender": "feminine"}]

wished output: 120 Mary feminine

I tried this solution \w\b\w+\b(?z!\id|name|gender)

but without any success.

victor
  • 5
  • 3
  • Do you want to get the output **specifically** from `"id"`, `"name"` or `"gender"` or just any key whatsoever? – Tiberiu Zulean Oct 31 '19 at 22:37
  • 4
    This is what JSON Parsers are for. – PM 77-1 Oct 31 '19 at 22:40
  • `[:]\s(\".*\")`? – Shaya Ulman Oct 31 '19 at 22:41
  • my original idea was to select every word "\w" and try to exclude the strings "id", "name", and "gender" from the match – victor Oct 31 '19 at 22:55
  • [:]\s(\".*\") this expression is better than any other that i've came up with but its returning : "120"; : "Mary"; : "Feminine" and only wish for the raw values – victor Oct 31 '19 at 23:02
  • This is a bad idea unless you know that the JSON is very restricted in its format. – Barmar Oct 31 '19 at 23:13
  • Put the capture group inside the double quotes if you don't want to include them in the result. – Barmar Oct 31 '19 at 23:14
  • You could actually be asking something a bit more specific as per @TiberiuZulean. But this question is kind of a meme, https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags TL;DR they are different kinds of language in the Chomsky hierarchy. – AER Oct 31 '19 at 23:25

1 Answers1

0

const s = '[{"id": "120","name": "Mary","gender": "feminine"}]'
const result = JSON.parse(s).map(x => Object.values(x).join(' '))
console.log(result)
Jacek Rojek
  • 1,082
  • 8
  • 16