This question already has answers here:
How can you encode a string to Base64 in JavaScript?
(33 answers)
Closed 2 years ago.
I want to encode my array contents with base64 if possible in javascript (and then decode later).
Example:
var array = ["stack", "overflow"]
// base64.encode(array)
Code:
var array = ["stack", "overflow"]
array.map(btoa);
In order to use the well-known function btoa, you'll first have to convert your array to string, in such a way that you can reverse the operation. JSON would be the string format to go for.
So to encode do:
base64 = btoa(JSON.stringify(array))
To decode do:
JSON.parse(atob(base64))
Related
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 ",".
This question already has answers here:
How to strip type from Javascript FileReader base64 string?
(5 answers)
What do 'lazy' and 'greedy' mean in the context of regular expressions?
(13 answers)
Closed 3 years ago.
I have a base64 data
data:application/pdf;base64,JVBERi0xLj........
I want to get the rest of the data like
JVBERi0xLj........
Following not working, wonder what is the correct approach?
const express = /data:(.*?);base64,(.*?)/g;
const arr = express.exec(base64);
You can simplify and match all the data uri standard :
:([^;]*)[^,]+,(.*)
Your Regex will not match other data urls that are also standard like :
data:text/vnd-example+xyz;foo=bar;base64,R0lGODdh.
data:text/plain;charset=UTF-8;page=21,the%20data:1234,5678
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,'\\');
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.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Convert JS object to JSON string
Store comma separate values into array
I have a string containing values separated with commas:
"1,4,5,11,58,96"
How could I turn it into an object? I need something like this
["1","4","5","11","58","96"]
This will convert it into an array (which is the JSON representation you specified):
var array = myString.split(',');
If you need the string version:
var string = JSON.stringify(array);
In JSON, numbers don't need double quotes, so you could just append [ and ] to either end of the string, resulting in the string "[1,4,5,11,58,96]" and you will have a JSON Array of numbers.
make it an array
var array = myString.split(',');