Convert String to nested array of characters [duplicate] - javascript

This question already has answers here:
Parsing string as JSON with single quotes?
(10 answers)
Closed 6 years ago.
I have a string passed to me like
var str = "['♫', ['►', 'Play']]";
Multiple entries can be there
Need to convert it to Array.
How can It be done?
PS: No eval() please. and also no Jquery
Note: I have tried JSON.parse, but it didn't work for me. It gives following error:
Unexpected token ' in JSON at position 1

JSON.parse(str.replace(/'/g, '"'))
In other words, fix the quotes so that it is valid JSON, then you will be able to use JSON.parse.

Related

Extract the array represented as a string [duplicate]

This question already has answers here:
Parsing string as JSON with single quotes?
(10 answers)
Execute JavaScript code stored as a string
(22 answers)
Closed 1 year ago.
i have a string as below and i would like to extract the array between the quotes.
mystring = "['str1','str2']"
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 ?
function parseString(string) {
return string
.split(",")
.map((str) => str.replace("[", "").replace("]", "").replaceAll("'", ""));
}
This assumes none of array indexes includes character ",".

RegExp with variable in a string format [duplicate]

This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Javascript Regex: How to put a variable inside a regular expression? [duplicate]
(9 answers)
Closed 2 years ago.
I created a RegExp that works as expected however I can't get it to work in a string format
var regex = new RegExp(/IN1\|2.*\r/g);
The regex is supposed to match the line number which will be taken from variable, in above example it would be line number 2. My question is how do I get it to a string format with a variable inside it?
I tried following but it just doesn't work: "IN1\|" + lineNumber + ".*\x0d//g"
Below is a text in case anyone wants to try:
IN1|**1**||QQ|Noth||||||||20190413|20190413||Self\r
IN1|**2**||QQ|Noth||||||||20190413|20190413||Self\r
IN1|**3**||QQ|LHS||||||||20200506|""||Private|||||||||||||||||||||2342344\r
Thank you.

string escaping in json object [duplicate]

This question already has answers here:
How should I escape strings in JSON?
(18 answers)
Closed 3 years ago.
I have this object that I send in response as json.
{"__type":"http:\\/\\/example.com\\/contracts\\/documents\\/rendering\\/instructions\\/1\\/0"}
I want response to be:
{"__type":"http:\/\/example.com\/contracts\/documents\/rendering\/instructions\/1\/0"}
but I get this:
{"__type":"http:\\/\\/example.com\\/contracts\\/documents\\/rendering\\/instructions\\/1\\/0"}
How do I escape string correctly, so I can get response string with only one backslash?
Do this:
let str='{"__type":"http:\\/\\/example.com\\/contracts\\/documents\\/rendering\\/instructions\\/1\\/0"}'
let result = str.replace(/\\/g,'\\');

How to get numbers in a string separated with commas and save each of them in an Array in Javascript [duplicate]

This question already has answers here:
How to split comma separated string using JavaScript? [duplicate]
(4 answers)
Closed 5 years ago.
I have a string of ID's wherein they're being separated with commas.
For example, the string is:
"15,14,12,13"
How can I extract the numbers/id's from this string and save each of them in a JSON or array to be something like this
Array: {
15,
14,
12,
13
}
I don't know how it's done using regex or string manipulation. Please advice.
use split & map & parseInt methods.
var numbers="15,14,12,13";
var result=numbers.split(',').map(function(number){
return parseInt(number);
});
console.log('convert '+JSON.stringify(numbers)+" to array:"+JSON.stringify(result));
Use eval method
var numbers="15,14,12,13";
var result=eval("["+numbers+"]");
console.log('convert '+JSON.stringify(numbers)+" to array:"+JSON.stringify(result));

Weird error on trying to parse an stringed array with JSON [duplicate]

This question already has answers here:
jQuery.parseJSON single quote vs double quote
(4 answers)
Closed 6 years ago.
I'm storing some information in Base64 encoded python lists, then decode them in javascript. However it does not parse my "list" as an array (the syntax is the same) because it gives me this error:
SyntaxError: JSON.parse: unexpected character at line 1 column 2 of
the JSON data
So it turns out, myString = "['foo']" returns this error, but myString = '["foo"]' works just fine. (In firefox at least)
Why does this happen? It makes zero sense, the quotation marks are not the same, so why does it throw an error?
Python always returns the string wrapped in "" and the actual contents of the list wrapped in '' so there is no way to change that.
JSON uses " to wrap strings, not ', thus 'foo' is not valid JSON string.

Categories