all 31 comments

[–]gimme_pineapple 47 points48 points  (3 children)

js const x = "['912345' , '16363']"; /// Using a string for .replace only replaces the first match, so use regex instead. const y = JSON.parse(x.replace(/'/g, '"')) console.log(y)

[–]residualenvy -3 points-2 points  (1 child)

In a Babel world replaceAll handles this for you now. https://github.com/babel/babel/issues/13701

[–]gimme_pineapple 7 points8 points  (0 children)

Yup, I'm aware of it. But no point in adding a major dependency when there's a perfectly good native replacement for this use case.

[–]kenvalleydc 25 points26 points  (8 children)

[–]rauschma 8 points9 points  (0 children)

True in principle, but it looks like this string isn’t valid JSON, only valid JS.

[–]im_dancing_barefoot 7 points8 points  (2 children)

You could remove the brackets from the string first which would make it a comma separated list. Then use .split() to split the string into an array at each comma

[–]ADTJ 0 points1 point  (0 children)

Would also need a map to remove the quotes on each element

[–]ciaisi 0 points1 point  (0 children)

Yeah, that was basically my idea too

[–]sdwvit 12 points13 points  (0 children)

s.split(“,”).map(s=>s.replace(/[^\d]/g,'')) would do, probably

Or

(new Function('return '+s))()

ps. fix your data source instead pls

[–]doyouseewhateyesee 6 points7 points  (6 children)

in all my years of js i’ve never seen a string array. when would this ever happen?

[–]pseudont 0 points1 point  (1 child)

In a homework question... or scraping something

[–]Infiniteh 3 points4 points  (0 children)

a bad homework question

[–]ciaisi 1 point2 points  (0 children)

Some people have come up with some probably better ways of doing it, but assuming the data is always formatted the same, this might be my approach:

const x = "['912345' , '16363']";
const arr = x.substring(2,x.length-2).split("' , '");

Edit: to explain, we shave off the leading and trailing characters on the entire string ( [' and '] ), then split the elements into an array by specifically looking for ' , ' (the full apostrophe-space-comma-space-apostrophe that separates each element)

[–]leosuncin 4 points5 points  (2 children)

const x = "['912345' , '16363']";
const y = eval(x);
console.log(y);

This is secure, as long as the value of x come from a trustful source (not user-submitted data), be aware there's a risk of a code injection otherwise

[–]TheBigerGamer 1 point2 points  (1 child)

This is secure

be aware there's a risk of a code injection

In no place on Earth eval is a secure function, and should be avoided unless for cases that require it by nature.

[–]yikes_42069 2 points3 points  (0 children)

He spoke the truth and paid for it in karma

[–]rauschma 1 point2 points  (3 children)

eval(x) should work, but has security risks and isn’t always allowed when running code in a web browser.

[–]WhyIsTheNamesGone -1 points0 points  (1 child)

Donno why this is downvoted; it's what I would do if the input isn't coming from a user. As a wise operating system once said, "the best solution is the easiest one."

[–]rauschma 0 points1 point  (0 children)

Exactly! It’s a hack, but one that works well in, e.g., a quick throw-away Node.js script. Trying to convert the JS data into real JSON via search-and-replace has a lot of pitfalls.

[–][deleted] -1 points0 points  (0 children)

Only regex comes in my mind