1

I have a JSON string:

x = '{"userId":"foo","traits":{"email":"[email protected]"},"type":"identify"}'

that I want to get certain values from. I tried regex:

So far I have

anonId = x.match(/\"anonymous_id\"\:(.*?)/)?[1]
email = x.match(/\"email\"\:\"(.*?)\"/)?[1]
userId = x.match(/\"userId\"\:\"(.*?)\"/)?[1]
type = x.match(/\"type\"\:\"(.*?)\"/)?[1]

which is ugly and inefficient, but when I attempt to combine them:

[_, a, b, c, d] = x.match(/\"anonymous_id\"\:(.*?)|\"userId\"\:(.*?)|\"email\"\:(.*?)|\"type\"\:(.*?)/g)

the results that are returned are the entire group, instead of just the matched parts.

I want a,b,c,d to equal the value of keys, but instead I get:

Wanted:
**>> ["foo","[email protected]","identify"]**
Actual results:
>> ["userId":"foo","email":"[email protected]","type":"identify"]

Is there any way to achieve this in one line regex?

--- UDPATE ----

I ended up going with

  rxp = /\"user_id\"\:\"(.*?)\"|\"anonymous_id\"\:\"(.*?)\"|\"type\"\:\"(.*?)\"/g

  anonId = null
  userId = null
  type = null

  while (arr = rxp.exec(bdy)) isnt null
    userId = arr[1] if arr[1]
    anonId = arr[2] if arr[2]
    type = arr[3] if arr[3]

FWIW I'm avoiding using JSON.parse because I'm processing thousands of these and as I only need a small piece of it, I don't want the slowness of JSON.parse to impact the servers unnecessarily.

6
  • 6
    Why can't you use JSON.parse() and find values by doing iteration? Commented Mar 1, 2016 at 6:11
  • Your question title Javascript match extract multiple values string in hardly related to what you're actually asking for. Can you improve? Commented Mar 1, 2016 at 6:54
  • @RokoC.Buljan i wasn't sure how to phrase it exactly, open to suggestions though Commented Mar 2, 2016 at 2:19
  • You don't even need to iterate after doing JSON.parse. You just need to access the relevant properties with obj.prop. Commented Mar 2, 2016 at 3:05
  • You're assuming that JSON.parse is going to be slower than regexp; have you tested that? In fact, JSON parsers are usually quite fast. Anyway, if you have an "answer" to your own question, please add it as an answer, not as part of the question. Commented Mar 2, 2016 at 3:09

2 Answers 2

2
try {
   var parsed = JSON.parse(x);
   anonId = parsed.anonymous_id;
} catch (ex) {
  //invalid json
}

That should work unless you have invalid JSON coming in. And then you might want to consider Regex but even then you might want to look at templates.

Sign up to request clarification or add additional context in comments.

Comments

0

Try using RegExp /[a-z@.]+(?=",|"})/ig at single call to .match()

var x = '{"userId":"foo","traits":{"email":"[email protected]"},"type":"identify"}';
var res = x.match(/[a-z@.]+(?=",|"})/ig);
console.log(res);

15 Comments

@RokoC.Buljan See updated post. Should match all values of properties of object at OP
@guest271314 your answer was very very close to what i needed, thanks! Unfortunately your function would return values based on the order of the data provided, and I wouldn't really be able to know which value is userId, email etc as the data doesn't come from something i can control
It's a mystery why you would propose to parse JSON with regexp.
@torazaburo "It's a mystery why you would propose to parse JSON with regexp" Requirement of Question; see OP at "Is there any way to achieve this in one line regex?"
How would you answer a question asking the best type of gun to shoot yourself in the head with?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.