3

My string is

"[1,2,3]"

I want to get output in the below format

[1,2,3]

I want to extract array present in my string.

1

3 Answers 3

4

Just parse it with JSON.parse method like this ;

JSON.parse("[1,2,3]")
Sign up to request clarification or add additional context in comments.

Comments

3

You can use eval:

try:

eval("[1,2,3]")
=> [1, 2, 3]

1 Comment

You shouldn't suggest to use eval without warning about the risks.
0

Ok, try it:

"[1,2,3]".scan(/\w/).map{|x| x.to_i}

1 Comment

This answer turned up in the low quality review queue, presumably because you don't provide any explanation of the code. If this code answers the question, consider adding adding some text explaining the code in your answer. This way, you are far more likely to get more upvotes — and help the questioner learn something new.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.