Javascript Trying to parse string into an array [duplicate] - javascript

This question already has answers here:
Parsing string as JSON with single quotes?
(10 answers)
Closed 2 years ago.
I am having a variable which stores a string like this
var colorArr="['#3f67c5', '#cb4728', '#f19d39', '#459331', '#984830', '#8C2094']"
I am trying to convert this string into an array by
var result = JSON.parse(colorArr)
But I keep on getting the following error
"SyntaxError: Unexpected token ' in JSON at position 1
Is there a way by which I can convert this string into a proper array?
Thanks in advance

Use single quotes and put double quotes inside of it
var colorArr= '["#3f67c5", "#cb4728", "#f19d39", "#459331", "#984830", "#8C2094"]';
var result = JSON.parse(colorArr)
console.log(result)

You can use regex to replace and split.
let colorArr="['#3f67c5', '#cb4728', '#f19d39', '#459331', '#984830', '#8C2094']";
let myArray = colorArr.replace(/'/g,'"')
console.log(JSON.parse(myArray))

try like this
var colorArr='["#3f67c5", "#cb4728", "#f19d39", "#459331", "#984830", "#8C2094"]';
JSON.parse(colorArr)

Related

How to Convert Single quoted elements of Array into just comma separated string [duplicate]

This question already has answers here:
Easy way to turn JavaScript array into comma-separated list?
(22 answers)
Javascript join() array
(1 answer)
Closed 1 year ago.
I have a array like
let array = ["MR','JR','IR','MY"]
I want to convert it into a comma separated String like
string = MR,JR,IR,MY
Any help is appreciated and Thanks in advance
You need to use the join method with comma as a separator
let string = array.join(',')
EDIT:
Actually your array have only one position, so you are not joining anything. You only need to replace the character ' and you are done.
let string = array[0].replace(new RegExp("'", "g"),"")

Prevent back slash escaping from the string in Javascript [duplicate]

This question already has answers here:
Escaping backslash in string - javascript
(5 answers)
Closed 3 years ago.
I have a code which replaces the back slashes as empty string
var data = '<strong>Welcome</strong>\(x = {-b';
document.write(data);
I am getting result like this:
Welcome(x = {-b
I am expecting result like this without modifying string value
Welcome\(x = {-b
I used the same string to display in an id as html content.
document.getElementById("test").innerHTML = '<strong>Welcome</strong>\(x = {-b';
It always replacing slashes by empty string. the string.split method did not helped me to solve this
var data = '<strong>Welcome</strong>\\(x = {-b';
document.write(data);
try this...

find and replace '%20' with a space in a string javascript [duplicate]

This question already has answers here:
Javascript replace all "%20" with a space
(7 answers)
Closed 4 years ago.
I'm having some trouble trying to figure this out,
basically I have a url string like so this%20is%20a%20string now what I want to do is find and replace all instances of %20 and replace with a space so the string then becomes this is a string.
Now I've tried to do something like this..
if(string.includes('%20')) {
const arr = str.split('%20');
}
which splits the string into an array, but I'm not sure how I can then turn the array of seperate strings into a full string with spaces between each word.
Any help would be appreciated.
Using regex,
str.replace(/%20/g, ' ');
Just use join:
str.split('%20').join(" ")
let val = "this%20is%20a%20string".replace(/%20/g, ' ');
alert(val);
replace

Converting string to integer array [duplicate]

This question already has answers here:
Convert JSON string to Javascript array [duplicate]
(4 answers)
Closed 5 years ago.
I have an array of geodata stored as a string, and need to turn into into a array of numbers.
var input = "[34.1103897,-118.0398531]"
var output = [34.1103897,-118.0398531]
Not sure the best way to do this. Any tips/ suggestions appreciated!
The easiest way to convert an array of that format is to use JSON.parse.
Simply:
var input = "[34.1103897,-118.0398531]";
var output = JSON.parse(input);
Assuming the data is always formatted with no whitespace, you could simply remove the first and last characters, and split on ',':
var input = '[34.1303897,-118.0398591]';
var output = input.slice(1, -1).split(',').map(parseFloat);
Alternatively, that syntax is technically valid JSON syntax, so you could just:
var input = '[34.1303897,-118.0398591]';
var output = JSON.parse(input);
However, this could be dangerous depending on where the data comes from.
JSON.parse is the obvious choice, but there is also manual parsing with a regular expression (the following assumes you might also want to match a leading '+'):
var input = "[34.1103897,-118.0398531]"
var output = (input.match(/[+-]?[\d\.]+/g) || []).map(Number);
console.log(output)
You might consider validating the input or the resulting output regardless of the method chosen to convert it to an array.

How to extract numbers from string in Javascript using regex [duplicate]

This question already has answers here:
Find and get only number in string
(4 answers)
Closed 8 years ago.
I have the following string
/Date(1317772800000)/
I want to use a Javascript regular expression to extract the numerical portion of it
1317772800000
How is this possible?
That should be it
var numPart = "/Date(1317772800000)/".match(/(\d+)/)[1];
No need for regex. Use .substring() function. In this case try:
var whatever = "/Date(1317772800000)/";
whatever = whatever.substring(6,whatever.length-2);
This'll do it for you: http://regex101.com/r/zR0wH4
var re = /\/Date\((\d{13})\)\//;
re.exec('/Date(1317772800000)/');
=> ["/Date(1317772800000)/", "1317772800000"]
If you don't care about matching the date portion of the string and just want extract the digits from any string, you can use this instead:
var re = /(\d+)/;
re.exec('/Date(1317772800000)/')
["1317772800000", "1317772800000"]

Categories

Resources