Javascript Regex Splitting - javascript

The following gives an array of length 1 with an empty string in it. Why?
var tokens = AN_EMPTY_STRING.split("ANY_REGEX");
I expect this should give an array with no elements in it. What is the logic of this?

This is just how split works (from the docs)
The split() method returns the new array.
When found, separator is removed from the string and the substrings
are returned in an array. If separator is not found or is omitted, the
array contains one element consisting of the entire string. If
separator is an empty string, str is converted to an array of
characters.
It doesn't matter what the seperator is, if it's there and not found, an '' (empty string) will return it's contents in the form of an array element so your return value will always be atleast '' (an empty string).
there seem to be some odd things here (IMO anyway)
''.split('')
returns [] with a .length of 0
''.split(/b/)
returns [""] with a .length property of 1
since the seperator (b) isn't in the string (working as intended according to the docs)

Related

Why is the first match empty when using a split regex? [duplicate]

I don't understand this behaviour:
var string = 'a,b,c,d,e:10.';
var array = string.split ('.');
I expect this:
console.log (array); // ['a,b,c,d,e:10']
console.log (array.length); // 1
but I get this:
console.log (array); // ['a,b,c,d,e:10', '']
console.log (array.length); // 2
Why two elements are returned instead of one? How does split work?
Is there another way to do this?
You could add a filter to exclude the empty string.
var string = 'a,b,c,d,e:10.';
var array = string.split ('.').filter(function(el) {return el.length != 0});
A slightly easier version of #xdazz version for excluding empty strings (using ES6 arrow function):
var array = string.split('.').filter(x => x);
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
Use String.split() method with Array.filter() method.
var string = 'a,b,c,d,e:10.';
var array = string.split ('.').filter(item => item);
console.log(array); // [a,b,c,d,e:10]
console.log (array.length); // 1
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/split
trim the trailing period first
'a,b,c,d,e:10.'.replace(/\.$/g,''); // gives "a,b,c,d,e:10"
then split the string
var array = 'a,b,c,d,e:10.'.replace(/\.$/g,'').split('.');
console.log (array.length); // 1
That's because the string ends with the . character - the second item of the array is empty.
If the string won't contain . at all, you will have the desired one item array.
The split() method works like this as far as I can explain in simple words:
Look for the given string to split by in the given string. If not found, return one item array with the whole string.
If found, iterate over the given string taking the characters between each two occurrences of the string to split by.
In case the given string starts with the string to split by, the first item of the result array will be empty.
In case the given string ends with the string to split by, the last item of the result array will be empty.
It's explained more technically here, it's pretty much the same for all browsers.
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);
// ↪ [""]
Well, split does what it is made to do, it splits your string. Just that the second part of the split is empty.
Because your string is composed of 2 part :
1 : a,b,c,d,e:10
2 : empty
If you try without the dot at the end :
var string = 'a,b,c:10';
var array = string.split ('.');
output is :
["a,b,c:10"]
You have a string with one "." in it and when you use string.split('.') you receive array containing first element with the string content before "." character and the second element with the content of the string after the "." - which is in this case empty string.
So, this behavior is normal. What did you want to achieve by using this string.split?
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

How Array.join works if separator is an array in Javascript?

i can't understand how Array.join works if separator is an array in Javascript ? I find a detail explanation how it works. My example below
arr1 = ['a','b','c'];
arr2 = ['d', 'e', 'f'];
arr1.join(arr2);
//Output
"ad,e,fbd,e,fc"
The join method accepts a separator and you're passing an array as separator. So, the join method will first transform it to a string: 'd,e,f'
Thus,
ad,e,f
bd,e,f
c // last remains same - no join
// as you may do like ['a','b','c'].join('-') results a-b-c
And final result:
ad,e,fbd,e,fc
You may read this note on docs:
Separator
Specifies a string to separate each pair of adjacent elements of the array. The separator is converted to a string if necessary. If omitted, the array elements are separated with a comma (","). If separator is an empty string, all elements are joined without any characters in between them.
JavaScript is a flexible language. If it will receive something other than it is expecting, it will try to convert the passed value into a form / type that it is expecting.
As from Docs, Array's .join() method expects a string in argument to be used as a separator for joining array elements. In case you will pass it an array, JavaScript will convert this array into a string internally by calling .toString() method of Arrays. And this returned string i.e d,e,f will be used as a separator to join elements in outer array.

javascript getting a faulty result using a regular expression

In my web page, I have:
var res = number.match(/[0-9\+\-\(\)\s]+/g);
alert(res);
As you can see, I want to get only numbers, the characters +, -, (, ) and the space(\s)
When I tried number = '98+66-97fffg9', the expected result is: 98+66-979
but I get 98+66-97,9
the comma is an odd character here! How can eliminate it?
Its probably because you get two groups that satisfied your expression.
In other words: match mechanism stops aggregating group when it finds first unwanted character -f. Then it skips matching until next proper group that, in this case, contains only one number - 9. This two groups are separated by comma.
Try this:
var number = '98+66-97fffg9';
var res = number.match(/[0-9\+\-\(\)\s]+/g);
// res is an array! You have to join elements!
var joined = res.join('');
alert(joined);
You're getting this because your regex matched two results in the number string, not one. Try printing res, you'll see that you've matched both 98+66-979 as well as 9
String.match returns an array of matched items. In your case you have received two items ['98+66-97','9'], but alert function outputs them as one string '98+66-97,9'. Instead of match function use String.replace function to remove(filter) all unallowable characters from input number:
var number = '98+66-97fffg9',
res = number.replace(/[^0-9\+\-\(\)\s]+/g, "");
console.log(res); // 98+66-979
stringvariable.match(/[0-9\+\-\(\)\s]+/g); will give you output of matching strings from stringvariable excluding unmatching characters.
In your case your string is 98+66-97fffg9 so as per the regular expression it will eliminate "fffg" and will give you array of ["98+66-97","9"].
Its default behavior of match function.
You can simply do res.join('') to get the required output.
Hope it helps you
As per documents from docs, the return value is
An Array containing the entire match result and any parentheses-captured matched results, or null if there were no matches.
S,your return value contains
["98+66-97", "9"]
So if you want to skip parentheses-captured matched results
just remove g flag from regular expression.
So,your expression should like this one
number.match(/[0-9\+\-\(\)\s]+/); which gives result ["98+66-97"]

How to remove double quotes from jquery array

I have a variable which contains the values like this ..
["09:09:49", "00:14:09", "00:05:50", "02:38:02", "01:39:28"]
Now as per my need i have to formate like this ..
[09:09:49, 00:14:09, 00:05:50, 02:38:02, 01:39:28]
for this i tried
callduration=[];
callduration=["09:09:49", "00:14:09", "00:05:50", "02:38:02", "01:39:28"];
var newstring = callduration.replace(/\"/g,'');
But it is giving error ..
TypeError: callduration.replace is not a function
var newstr=callduration.replace(/\"/g,'');
Please help me.
Thanks in advance..
First off, you must note that callduration is an array. Arrays do not have a replace method, hence the error.
As mentioned by #Felix Kling, the quotes are just string delimiters. They are not part of the string values contained in your array of strings. For example, when accessing callduration[0] you will get a string containing the 09:09:49 sequence of characters.
However, if you really need a string in the requested format, here it is:
var callduration = ["09:09:49", "00:14:09", "00:05:50", "02:38:02", "01:39:28"];
var newstr = '[' + callduration.join(', ') + ']';
newstr; //"[09:09:49, 00:14:09, 00:05:50, 02:38:02, 01:39:28]"
Though this probably won't be of much use unless you have some very specific use case in mind.
callduration is an array. That means it contains a sequential, ordered list of items. Those items must be something that can exisdt in javascript. As your array exists like this:
["09:09:49", "00:14:09", "00:05:50", "02:38:02", "01:39:28"]
it is an array of strings. Each time value is represented by its own string. The quote marks are not actually part of the string - that' just how a string is represented when typing it.
If you want the array to be an array of something other than strings, you would need to specify what data type you want it to be. 09:09:49 as you've asked, it not a legal javascript piece of data.
Some choices that you could use:
An array of numbers where each number represents a time value (say milliseconds since midnight).
An array of Date objects.
If you have an array of strings now and you wanted to convert it to either of the above, you would loop through your existing array, parse the string you have now into an actual numeric time and then convert that into whatever numeric or object format you want to be in the array.

javascript split() array contains

While learning JavaScript, I did not get why the output when we print the array returned of the Sting.split() method (with regular expression as an argument) is as explained below.
var colorString = "red,blue,green,yellow";
var colors = colorString.split(/[^\,]+/);
document.write(colors); //this print 7 times comma: ,,,,,,,
However when I print individual element of the array colors, it prints an empty string, three commas and an empty string:
document.write(colors[0]); //empty string
document.write(colors[1]); //,
document.write(colors[2]); //,
document.write(colors[3]); //,
document.write(colors[4]); //empty string
document.write(colors[5]); //undefined
document.write(colors[6]); //undefined
Then, why printing the array directly gives seven commas.
Though I think its correct to have three commas in the second output, I did not get why there is a starting (at index 0) and ending empty string (at index 4).
Please explain I am screwed up here.
/[^\,]+/ splits on one or more characters that are not a comma. Thus, JavaScript will split your string on red, blue etc. The resulting leftovers, then, are the empty string at the beginning (the substring from index 0 to 0), the commas, and the empty string at the end. If you go out of bounds of the array you get undefined (as with any array).
red,blue,green,yellow
xxx xxxx xxxxx xxxxxx <-- x is what is being eaten during split, because it's the delimiter
You just want .split(","), which splits on commas, so that the commas are eaten and you are left with the colors.
Now, when you do document.write(someArray), the array is converted into a string so that it can be displayed. This effectively means someArray.join() is called, which by default puts commas in between. So you get commas joined by commas, resulting in even more commas.
When you print out the array, the different elements of the array are also separated by commas. So your output are these 5 array elements:
[empty string],[comma],[comma],[comma],[empty string]
Amounting to 7 commas. The reason why you get commas and empty strings instead of colors is, that split will split at everything that matches (instead of giving you back everything that matches). So simply don't use regular expressions at all, but just split at ,:
var colors = colorString.split(',');
[^\,] <- this means anything BUT commas.
try
var colors = colorString.split(',');

Categories