Dont work split(). javascript [duplicate] - javascript

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));

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 to get entire array as string? [duplicate]

This question already has an answer here:
array join() method without a separator
(1 answer)
Closed 3 years ago.
I need to get entire array result as a clear string like: namegenderage, but can't figure out how
I tried:
var arr = ["name","gender","age"];
var string = arr.toString().replace(",","");
console.log(string);
log result should be string like namegenderage without quotes and brackets but I get this result "namegender,age"
You can just use the join() method:
var arr = ["name","gender","age"];
var string = arr.join('');
console.log(string);

javascript Regular Expressions [duplicate]

This question already has answers here:
Get the values from the "GET" parameters (JavaScript) [duplicate]
(63 answers)
Closed 6 years ago.
I have the following url
http://www.test.info/?id=50&size=40
How do I get the value of the url parameter with regular expressions in javascript . i need the size value and also need the url without &?
only
http://www.test.info/?id=50
Thanks
Consider using split instead of a regex:
var splitted = 'http://www.test.info/?id=50&size=40'.split('&');
var urlWithoutAmpersand = splitted[0];
// now urlWithoutAmpersand => 'http://www.test.info/?id=50'
var sizeValue = splitted[1].split('=')[1] * 1;
// now sizeValue => 40
Just use this as your regex
size.*?(?=&|$)
here is some code you can use
var re = /size.*?(?=&|$)/g;
var myArray = url.match(re);
console.log(myArray);
you also can do it like this:
var re = new RegExp("size.*?(?=&|$)", "g");
Here is a regex pattern you could use.
^(.+)&size=(\d+)
The first group will be the url up to right before the '&' sign. The second group will be the value of the size parameter. This assumes id always comes before size, and that there are only two parameters: id and size.

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

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'];

Replace string having , with $ [duplicate]

This question already has answers here:
JavaScript - Replace all commas in a string [duplicate]
(3 answers)
Closed 8 years ago.
Below is my string.When I console b it is showing as below output:
var a='602,315,805,887,810,863,657,665,865,102,624,659,636';
var b = a.replace(',',"$");
console.log(b);
output:
602$315,805,887,810,863,657,665,865,102,624,659,636
What should I do to replace complete commas in string to $.
Use regexp, /,/g with global flag
var a ='602,315,805,887,810,863,657,665,865,102,624,659,636';
var b = a.replace(/,/g,"$");
Example
This question already has an answer anyway i have provide a different approach
var var a ='602,315,805,887,810,863,657,665,865,102,624,659,636';
var change= '$'
a= a.split(',').join(change);
You can use String methods .split() and .join() to make an array then glue the pieces back together.
var b = a.split(',').join('$');
str.replace(/,/g,"$");
will replace , with $
DEMO

Categories