I don\'t understand this behaviour:
var string = \'a,b,c,d,e:10.\';
var array = string.split (\'.\');
I expect this:
consol
Well, split does what it is made to do, it splits your string. Just that the second part of the split is empty.
This is the correct and expected behavior. Given that you've included the separator in the string, the split function (simplified) takes the part to the left of the separator ("a,b,c,d,e:10") as the first element and the part to the rest of the separator (an empty string) as the second element.
If you're really curious about how split() works, you can check out pages 148 and 149 of the ECMA spec (ECMA 262) at http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf
try this
javascript gives two arrays by split function, then
var Val = "abc@gmail.com";
var mail = Val.split('@');
if(mail[0] && mail[1]) { alert('valid'); }
else { alert('Enter valid email id'); valid=0; }
if both array contains length greater than 0 then condition will true
A slightly easier version of @xdazz version for excluding empty strings (using ES6 arrow function):
var array = string.split('.').filter(x => x);
According to MDN web docs:
Note: When the string is empty, split() returns an array containing one empty string, rather than an empty array. If the string and separator are both empty strings, an empty array is returned.
const myString = '';
const splits = myString.split();
console.log(splits);
// ↪ [""]
so; [String.prototype] split(separator, limit)
return to the normal limit parameter is empty.
"".split(",") // [""]
"a,b,c.".split(".", 1) // ["a,b,c"]
If you could not have done this too, without limit parameter. I think this is a normal operation...