0

I have data like

[{somevalue1},{somevalue2}]

I want to extract datawithin the braces

I used

var data = data.match(/[{][^\"]+[}]/g);

But I receive null value.

Santron Manibharathi
  • 576
  • 4
  • 11
  • 25
  • 2
    `.match` is a native JavaScript function, it has nothing to do with jQuery. You expression does not match because `[^a]` matches any character but `a`, so it matches up to `{somev` and then fails because the next character is `a` and not `}`. Why are you using `[^a]` at all? – Felix Kling Aug 01 '12 at 21:48
  • 1
    Just to verify...the format of the string you supplied looks similar to JSON, although the objects aren't exactly correct JavaScript objects. You're not trying to manually parse a JSON-encoded string, correct? If you are, I'd highly recommend using this library instead...https://github.com/douglascrockford/JSON-js/ – rybosome Aug 01 '12 at 22:02
  • @HerroRygar yes. I am trying to parse Json manually.. – Santron Manibharathi Aug 01 '12 at 22:53
  • If you use that library that I recommended above (written by the creator of the JSON standard, Douglas Crockford), all you will have to do is: `var data = JSON.parse(stringOfData);`. It's less error prone and safer...a much, much better alternative to trying to do it yourself. – rybosome Aug 01 '12 at 23:08
  • @HerroRygar yes. Thank you. But i try it just for learning. – Santron Manibharathi Aug 01 '12 at 23:15

2 Answers2

1

You can use this:

data = data.match(/\{([\w\s]*)\}/g);

\w will look for alphanumeric, and \s for spaces.

guiligan
  • 369
  • 4
  • 16
  • 2
    Why not use `[^}]*` instead of `[\w\s]` ? That'd account for special characters like `?` or `$` too. – Nicolas Stamm Aug 01 '12 at 21:58
  • `\{([^}]*)\}` would also be a possibility. Since I don't know what is the format that should be valid for the user, I preferred to just consider alphanumeric and spaces :) – guiligan Aug 01 '12 at 22:08
  • When i used it, it took {somevalule1},{somevalue2} as a single element. Hence i changed it to `data = data.match(/\{([^}]*)\}/g);` – Santron Manibharathi Aug 01 '12 at 22:48
0
data=data.match(/\{([^}]*)\}/g);
Santron Manibharathi
  • 576
  • 4
  • 11
  • 25