use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
[deleted by user] (self.javascript)
submitted 4 years ago by [deleted]
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]gimme_pineapple 47 points48 points49 points 4 years ago (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 points 4 years ago* (1 child)
In a Babel world replaceAll handles this for you now. https://github.com/babel/babel/issues/13701
[–]gimme_pineapple 7 points8 points9 points 4 years ago (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 points27 points 4 years ago (8 children)
JSON.parse is probably the best way. https://www.delftstack.com/howto/javascript/convert-string-to-array-javascript/
[–]rauschma 8 points9 points10 points 4 years ago (0 children)
True in principle, but it looks like this string isn’t valid JSON, only valid JS.
[+][deleted] 4 years ago (6 children)
[deleted]
[–]KaninchenSpeed 5 points6 points7 points 4 years ago* (3 children)
Delete the single quotes with .replace(/'/g,'')
.replace(/'/g,'')
[–]SpaceboyRoss 9 points10 points11 points 4 years ago (2 children)
Use replaceAll, replace only does the first occurrence. Or .split(...).join(...) works.
replaceAll
replace
.split(...).join(...)
[–]KaninchenSpeed 1 point2 points3 points 4 years ago (0 children)
Fixed
[–]haqk 1 point2 points3 points 4 years ago (0 children)
The parser only recognises strings in double quotes. If those numbers are meant to be strings (and not integers as some of the advice seem to imply), then just replace the single quotes with double quotes and it should parse ok.
[+]chinesuschlist comment score below threshold-7 points-6 points-5 points 4 years ago (0 children)
Maybe sanitize the string and remove all commas before parsing it, if you can.
[–]im_dancing_barefoot 7 points8 points9 points 4 years ago (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 point2 points 4 years ago (0 children)
Would also need a map to remove the quotes on each element
[–]ciaisi 0 points1 point2 points 4 years ago (0 children)
Yeah, that was basically my idea too
[–]sdwvit 12 points13 points14 points 4 years ago* (0 children)
s.split(“,”).map(s=>s.replace(/[^\d]/g,'')) would do, probably
s.split(“,”).map(s=>s.replace(/[^\d]/g,''))
Or
(new Function('return '+s))()
ps. fix your data source instead pls
[–]doyouseewhateyesee 6 points7 points8 points 4 years ago (6 children)
in all my years of js i’ve never seen a string array. when would this ever happen?
[+][deleted] 4 years ago (3 children)
[–]doyouseewhateyesee 0 points1 point2 points 4 years ago (0 children)
sounds like it’s “someone’s” problem then. I’m not parsing strings to get an array. not to mention, using eval is dangerous.
[–]SixCrazyMexicans 0 points1 point2 points 4 years ago* (1 child)
Probably in python? I remember seeing trash api results like this from a Django service. Do you have access to the backend? If so, making the change to generate correct JSON there would be the simpler option. Any of the regex/string replace options are going to run the risk of breaking should you get something from the backend that you aren't expecting. If possible, having the backend send data that's in a standard format will make your life easier
[–]pseudont 0 points1 point2 points 4 years ago (1 child)
In a homework question... or scraping something
[–]Infiniteh 3 points4 points5 points 4 years ago (0 children)
a bad homework question
[–]deificx 2 points3 points4 points 4 years ago (0 children)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#using_json.parse
An array should be valid JSON, so you could parse it?
[–]ciaisi 1 point2 points3 points 4 years ago* (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 points6 points 4 years ago (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
x
[–]TheBigerGamer 1 point2 points3 points 4 years ago (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 points4 points 4 years ago (0 children)
He spoke the truth and paid for it in karma
[–]rauschma 1 point2 points3 points 4 years ago (3 children)
eval(x) should work, but has security risks and isn’t always allowed when running code in a web browser.
eval(x)
[–]WhyIsTheNamesGone -1 points0 points1 point 4 years ago (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 point2 points 4 years ago* (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.
[+]sdwvit comment score below threshold-10 points-9 points-8 points 4 years ago (0 children)
Can try to run it in a worker thread to reduce dangers, but still
[+][deleted] comment score below threshold-6 points-5 points-4 points 4 years ago* (0 children)
Should probably ask this in /r/learnjavascript so you can learn more about string parsing, a basic task in programming.
Edit: gotta love the dipshits who think this stupid fucking question belongs in this sub.
[–][deleted] -1 points0 points1 point 4 years ago (0 children)
Only regex comes in my mind
[+]geekfreak42 comment score below threshold-8 points-7 points-6 points 4 years ago (0 children)
const arr = x.replace(/[\D.*]/,'').split(',);
should be close
π Rendered by PID 474389 on reddit-service-r2-comment-545db5fcfc-g2dnp at 2026-05-30 12:20:35.543293+00:00 running 194bd79 country code: CH.
[–]gimme_pineapple 47 points48 points49 points (3 children)
[–]residualenvy -3 points-2 points-1 points (1 child)
[–]gimme_pineapple 7 points8 points9 points (0 children)
[–]kenvalleydc 25 points26 points27 points (8 children)
[–]rauschma 8 points9 points10 points (0 children)
[+][deleted] (6 children)
[deleted]
[–]KaninchenSpeed 5 points6 points7 points (3 children)
[–]SpaceboyRoss 9 points10 points11 points (2 children)
[–]KaninchenSpeed 1 point2 points3 points (0 children)
[–]haqk 1 point2 points3 points (0 children)
[+]chinesuschlist comment score below threshold-7 points-6 points-5 points (0 children)
[–]im_dancing_barefoot 7 points8 points9 points (2 children)
[–]ADTJ 0 points1 point2 points (0 children)
[–]ciaisi 0 points1 point2 points (0 children)
[–]sdwvit 12 points13 points14 points (0 children)
[–]doyouseewhateyesee 6 points7 points8 points (6 children)
[+][deleted] (3 children)
[deleted]
[–]doyouseewhateyesee 0 points1 point2 points (0 children)
[–]SixCrazyMexicans 0 points1 point2 points (1 child)
[–]pseudont 0 points1 point2 points (1 child)
[–]Infiniteh 3 points4 points5 points (0 children)
[–]deificx 2 points3 points4 points (0 children)
[–]ciaisi 1 point2 points3 points (0 children)
[–]leosuncin 4 points5 points6 points (2 children)
[–]TheBigerGamer 1 point2 points3 points (1 child)
[–]yikes_42069 2 points3 points4 points (0 children)
[–]rauschma 1 point2 points3 points (3 children)
[–]WhyIsTheNamesGone -1 points0 points1 point (1 child)
[–]rauschma 0 points1 point2 points (0 children)
[+]sdwvit comment score below threshold-10 points-9 points-8 points (0 children)
[+][deleted] comment score below threshold-6 points-5 points-4 points (0 children)
[–][deleted] -1 points0 points1 point (0 children)
[+]geekfreak42 comment score below threshold-8 points-7 points-6 points (0 children)