0

I need to extract a JSON string that sits inside a bigger string (I know, bad API response that I just have to deal with). Anyway I built a single regex to read everything inside {} and it works in all cases tested, except for the actual API response in the snippet below.

Any ideas why it's not working and how to make it work?

let dd = 'Error: Internal JSON-RPC error.\n{\n  "code": 3,\n  "message": "execution reverted: This limited season is sold out. Please wait for the next season.",\n  "data": "0x08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000004154686973206c696d6974656420736561736f6e20697320736f6c64206f75742e20506c65617365207761697420666f7220746865206e65787420736561736f6e2e00000000000000000000000000000000000000000000000000000000000000"\n}';

alert(RegExp(/{.*}/).exec(dd))

4
  • a) don't call RegExp() on a regular expression literal b) don't regex-match anything. Just split off the first line and treat the rest as JSON. Commented Oct 25, 2021 at 23:24
  • 1
    Your problem is that . does not match linebreaks. Commented Oct 25, 2021 at 23:25
  • I like the idea of ignoring the first line, seems simple enough, but.. what if a genius behind the API returns a 2-line response before the json. I don't like regex but it seems a bit more resilient in this case. Would you agree? Commented Oct 25, 2021 at 23:39
  • 1
    Well of course the API is just broken (and you should file an issue to get it fixed), but I'd think the likelihood that the error message (that is prepended to the json) contains a { is higher than the likelihood of it containing a linebreak. YMMV. Commented Oct 25, 2021 at 23:43

4 Answers 4

1

You need to add an s flag as the second argument to your RegExp constructor:

s - ("dotAll") Allows . to match newlines.

let dd = 'Error: Internal JSON-RPC error.\n{\n  "code": 3,\n  "message": "execution reverted: This limited season is sold out. Please wait for the next season.",\n  "data": "0x08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000004154686973206c696d6974656420736561736f6e20697320736f6c64206f75742e20506c65617365207761697420666f7220746865206e65787420736561736f6e2e00000000000000000000000000000000000000000000000000000000000000"\n}';

alert(RegExp('{.*}', 's').exec(dd))

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/RegExp

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

Comments

1

The content is not matched between multiple lines, so you can use the /s modifier to include new lines. Also Regexp() is not needed when using regexp syntax (/{.*}/s is the same as Regexp('{.*}','s')):

alert(/{.*}/s.exec(dd))

let dd = 'Error: Internal JSON-RPC error.\n{\n  "code": 3,\n  "message": "execution reverted: This limited season is sold out. Please wait for the next season.",\n  "data": "0x08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000004154686973206c696d6974656420736561736f6e20697320736f6c64206f75742e20506c65617365207761697420666f7220746865206e65787420736561736f6e2e00000000000000000000000000000000000000000000000000000000000000"\n}';

alert(/{.*}/s.exec(dd))

Comments

0

Try this expression which matches everything.

/\{[\s\S]*\}/.exec(dd)

The one you created didn't work because it contains linebreaks \n.

Comments

0

With another approach using .match() will actually return the match against the string you're want to extract, which is inside {curly braces}. with two flags s to enable searching new lines and g for searching globally

dd.match(/\{(.*)?\}/sg)[0]

Result {\n "code": 3,\n "message": "execution reverted: T…0000000000000000000000000000000000000000000000"\n}

Comments

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.