Title: Extract the array represented as a string
Tags: javascript
Question: i have a string as below and i would like to extract the array between the quotes.
mystring = &quot;['str1','str2']&quot;
I tried it with eval and i do not want to use eval in my code. is there any other neat way to do this ?
Comment: May use combination of slice and split

Comment: Yes. this is not a valid JSON and i dont want to use eval either.

Comment: @evolutionxbox sometimes it can be integers as well. like "[5,6]"

Comment: @SebastianSimon This is working.. Replace the single quote into double quotes and then parsing it.. thank you.

Comment: @evolutionxbox Yes it does. and thank you.

Comment: #@SebastianSimon Thank you for the link

Comment: @mousetail the string is sadly not valid JSON.

Comment: @boredbear153 is the data in the string always an array of strings?

Comment: You can use `JSON.parse`

Comment: @evolutionxbox sad

Comment: See [Parsing string as JSON with single quotes?](/q/36038454/4642212).


Here is another answer: function parseString(string) {
return string
.split(&quot;,&quot;)
.map((str) =&gt; str.replace(&quot;[&quot;, &quot;&quot;).replace(&quot;]&quot;, &quot;&quot;).replaceAll(&quot;'&quot;, &quot;&quot;));
This assumes none of array indexes includes character &quot;,&quot;.
Comment for this answer: this works as expected. i will also get the input as "[5,6]". In this case, the expected output is  [5,6]. However, with the above function it is ["5","6"]. Do i have to transform it again checking if each element is a number or string ?
Comment for this answer: @Erfan Yes. i m considering the answer from the below link
https://stackoverflow.com/questions/36038454/parsing-string-as-json-with-single-quotes
Comment for this answer: @SebastianSimon I will request to provide valid JSOn. Not sure if it is possible in this case,  based on the outcome, i need to decide my next step.
Comment for this answer: @boredbear153, Yes, I think you need to do that. Also, I think there are quite a few other things that should be considered. Will be a complicated function in the end.
Comment for this answer: @boredbear153 Why not simply request valid JSON instead? Either use JSON or build your own custom parser.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 11, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2021:00:09/15/21', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<javascript>', 'Title': 'Extract the array represented as a string', 'ViewCount': 34, 'id': 69190101, 'user_AboutMe': '', 'user_AccountId': 9327301.0, 'user_DisplayName': 'boredbear153', 'user_Reputation': 241.0, 'user_Views': 39.0, 'user_id': 6924121.0}}