Home > Mobile >  How would I parse specific JSON parameters using Regular Expressions in Javascript?
How would I parse specific JSON parameters using Regular Expressions in Javascript?

Time:11-22

To make this question short and simple, I have the value "session" in my browsers local storage with this value:

{"id":992085,"token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiI5OTIwODUiLCJuYW1lIjoiYmNfZXg3bHRlZCBiY19leDdsdGVkYSIsImlhdCI6MTY2OTA0MT3fas48gj9joxNjc2ODE3OTc3LCJpc3MiOiJodHRwczovL3dvcmtzcGFjZS5maXZlcnIuY29tIiwiYW5kY29Vc2VySWQiOjk5MjA4NSwiYW5kY29Sb2xlIjoiRnJlZWxhbmNlclJvbGUifQ.QvEyJ9Gq3jWG0zVROFyKf8-EKIufMyi6Ljwc1ioh9DQ","lastLoginDate":"2022-11-21T14:43:00.000Z","role":0,"email":"[email protected]"}

I'm writing a very simple bit of javascript in which I would like to parse the 992085 value and the eyJ0eXAi..... value and combine them into 992085:eyJ0eXAi..... then base64 encode that value and make that it's own variable. The not-so-simple part is the regular expression needed to parse the values I'd like. I know basic regEx but I've never even tried regEx in javascript.

What would be the proper javascript regular expression for my scenario?

Once again, I've toyed with regEx in the past but I am lacking in knowledge to do something this complex. I know there are some serious regEx geniuses on here who would find this easy.

EDIT: This question has been answered appropriately. I have no idea why you guys downvote CONSTANTLY. It's obvious I'm a noob. If you HATE the question so much just move on.

CodePudding user response:

You would not do this with a regular expression in Javascript. Use the built in JSON parser

obj = JSON.parse(s);
result = obj.id   obj.token;
  • Related