Reading json object with key starting with '#' character [duplicate] - javascript

This question already has answers here:
How to access object property with invalid characters
(2 answers)
Closed 7 years ago.
My JSON structure is as follows
"{"Key":{"#text":"100150410150347261963/output/Five String.mp4"},"LastModified":{"#text":"2015-05-26T15:33:39.000Z"},"ETag":{"#text":"\"5e5fd36802186f81109a9adedcb802fe\""},"Size":{"#text":"18831126"},"StorageClass":{"#text":"STANDARD"}}"
This is my code
var data = JSON.parse("{"Key":{"#text":"100150410150347261963/output/Five String.mp4"},"LastModified":{"#text":"2015-05-26T15:33:39.000Z"},"ETag":{"#text":"\"5e5fd36802186f81109a9adedcb802fe\""},"Size":{"#text":"18831126"},"StorageClass":{"#text":"STANDARD"}}");
var key = data.Key;
Now I want to read the value '100150410150347261963/output/Five String.mp4' but the key to this value is '#text', which starts with a # character. How can I read this?
var value = key.#text;
or
var value = key.'#text';
is not working. Is there any way to read this value?
PS: Please ignore the escaping of double quote '"' characters

Use bracket notation:
var value = data.Key['#text'];

Related

Removing all spesific value in string of number in Node.js [duplicate]

This question already has answers here:
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Closed 1 year ago.
I want to remove all "0" in my string, not only the first same value, any suggest?
Why its work but just only the first code
var str = "90807005"
console.log(str.replace("0",""))
I try to read another source and say to use (/"something"/g, new) for change all same value, and its still not working
var str = "90807005"
console.log(str.replace(/"0"/g,""))
I want it to be str = "9875";
You can use String.replaceAll or a global flag in your regex:
var str = "90807005"
console.log(str.replaceAll("0","")) //replaceAll
console.log(str.replace(/0/g,"")) //global flag

how replace a value of cookie in cookie string using regular expression in javascript [duplicate]

This question already has answers here:
Capture value out of query string with regex?
(9 answers)
Closed 3 years ago.
i have a cookie string like this
'user=sravan;XSRF-TOKEN=1212143;session=random'
i need to check for the XSRD-TOKEN in the cookie string, if we have the XSRF-TOKEN in the string then need to replace the value with 'test'
expected new string is 'user=sravan;XSRF-TOKEN=test;session=random'
i tried this (?<=XSRF-TOKEN).*$ but it is selecting the entire string after XSRF-TOKEN=
You could use (?<=XSRF-TOKEN=)([^;]+), example:
const str = 'user=sravan;XSRF-TOKEN=1212143;session=random';
const processed = str.replace(/(?<=XSRF-TOKEN=)([^;]+)/, "test");
console.log(processed);
But a better solution will be to parse the cookies and recreate the string.
This should only only select up until ;
(?<=XSRF-TOKEN)[^;]+
Or if you only like to select whats after = to ;
(?<=XSRF-TOKEN=)[^;]+
'user=sravan;XSRF-TOKEN=1212143;session=random'

How to convert enclosed double quotes to single quotes in javascript? [duplicate]

This question already has answers here:
Parsing string as JSON with single quotes?
(10 answers)
Closed 3 years ago.
I am experiencing a very strange behavior of Javascript.
I get the data object in the form of a string from the server as shown below,
"{'id':1234, 'name'}"
When I try to parse this data using JSON.parse() it throws
JSON.parse: expected property name or '}' at line 1 column 2 of the JSON data
However changing the data to '{"id":1234, "name"}' will work.
But my question is how do I transform:
"{'id':1234, 'name'}" to '{"id":1234, "name"}'
in the javascript end? (I dont want to change any thing in the server).
Simply you need to replace the character(') as global, here the code:
var yourString = "{'id':1234, 'name'}";
yourString = yourString.replace(/\'/g, '"');
console.log(yourString);
And in vice versa:
var yourString = '"{\'id\':1234, \'name\'}"';
yourString = yourString.replace(/\'/g, '"');
yourString = yourString.replace(/\"/g, "'");
console.log(yourString);
Here other example with mixed characters( " and ' ):
var yourString = "{\"id':1234, 'name'}";
yourString = yourString.replace(/\'/g, '"');
console.log(yourString); // Automatically skips the right character(")

Dont work split(). javascript [duplicate]

This question already has answers here:
var name and window.name
(2 answers)
Closed 4 years ago.
there is a line.
how do I split it into an array via separator ",".
If you try to split, the same line is returned
var str = "1,2,3,4,5,6";
console.log(str);
console.log(typeof(str));
var name = str.split(",");
console.log(name);
console.log(typeof(name));
name is a global property of Window object that is accessible throughout the JavaScript runtime and this is always a String type so despite of str.split(',') returning array of elements, due to its default type it is converted back to the string values. Thus, you need to use different variable name say, _name.
var str = "1,2,3,4,5,6";
console.log(str);
console.log(typeof(str));
var _name = str.split(',');
console.log(_name);
console.log(typeof(_name));

JavaScript Regular Expression to get a url value [duplicate]

This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 8 years ago.
I need a Regular Expression which can meet my request below:
It can get characters between 'id=' and '&'
the character '&' may exist or not
here's a example, say I have two URL like below:
http://example.com?id=222&haha=555
http://example.com?id=222
The Regular Expression can get the key id's value 222 in both URL.
VERY THANKFUL if someone can help me solve this problem!!!
You could try the below regex to get the id value whether it is followed by a & symbol or line end $.
id=(.*?)(?=&|$)
The captured value was stored inside the group index 1.
DEMO
> var re = /id=(.*?)(?=&|$)/g;
undefined
> var str = 'http://example.com?id=222&haha=555';
undefined
> var m;
undefined
> while ((m = re.exec(str)) != null) {
... console.log(m[1]);
... }
222
Not the regexp way but this should also work
str.split("id=")[1].split('&')[0]

Categories