From: Lasse Reichstein Nielsen on
"Evertjan." <exjxw.hannivoort(a)interxnl.net> writes:

> Lasse Reichstein Nielsen wrote on 26 jun 2010 in comp.lang.javascript:
>
>>> var numberOfElements = varString.split(/;/).length;
>>
>> RegExp should be global.
>>
>
> Please explain.

My mistake. I mistakenly assumed that String.prototype.split with
a regexp argument checked the "global" flag of the regexp, and
only matched once if the flag was not set.
It doesn't. Rather it completely ignores the "global" flag, being
specified directly in terms of the underlying [[Match]] function.

Probably confused it with replace.
/L
--
Lasse Reichstein Holst Nielsen
'Javascript frameworks is a disruptive technology'

From: Evertjan. on
Lasse Reichstein Nielsen wrote on 26 jun 2010 in comp.lang.javascript:

> "Evertjan." <exjxw.hannivoort(a)interxnl.net> writes:
>
>> Lasse Reichstein Nielsen wrote on 26 jun 2010 in comp.lang.javascript:
>>
>>>> var numberOfElements = varString.split(/;/).length;
>>>
>>> RegExp should be global.
>>>
>>
>> Please explain.
>
> My mistake. I mistakenly assumed that String.prototype.split with
> a regexp argument checked the "global" flag of the regexp, and
> only matched once if the flag was not set.
> It doesn't. Rather it completely ignores the "global" flag, being
> specified directly in terms of the underlying [[Match]] function.
>
> Probably confused it with replace.

ok

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
From: Lasse Reichstein Nielsen on
Thomas 'PointedEars' Lahn <PointedEars(a)web.de> writes:

> May I suggest
>
> var count = (varArray.match(/;/g) || "").length + 1;
>
> instead? ;-)

That should be pretty effective.
I like it.

/L
--
Lasse Reichstein Holst Nielsen
'Javascript frameworks is a disruptive technology'

From: Asen Bozhilov on
Lasse Reichstein Nielsen wrote:
> He...(a)earthlink.net writes:

> > varArray = varValue.split(";");
> > varEls = varArray.length;
>
> > Is this reliable, or is there a better way?
>
> If there are no other semicolons than the ones separating your
> elements, it should split the string into those elements.
>
> It's a waste of time and space to make all those small strings
> if you don't need them.

I agree with you. The code of OP break optimizations, because hold
reference to returned array by `split'. The interesting cases are:

str.split(';').length;

Here I do not see any reasons to be created all these strings. There
is not reference to returned array, which is available by scope chain.
And `split' in this case is better to return empty array with length
property equal to numbers of substrings.

Another interesting case is:

str.split('').length;

Is there reason for `split' to create an array object with each
substrings or just to return the `this' value, which be used for
getting the value of `length' property?
From: Thomas 'PointedEars' Lahn on
Asen Bozhilov wrote:

> Thomas 'PointedEars' Lahn wrote:
>> Lasse Reichstein Nielsen wrote:
>> > var count = 0;
>> > for (var i = varArray.indexOf(";");
>> > i >= 0;
>> > i = varArray.indexOf(";", i + 1)){
>> > count++;
>> > }
>>
>> May I suggest
>>
>> var count = (varArray.match(/;/g) || "").length + 1;
>
> OP does not give definition what should mean an element. If element
> has fixed characters length, can be used:
>
> Math.ceil(str.length / (elSize + 1));

ACK

> Otherwise your approach is enough.

Except that it does not cover the case of 0 elements.

> Of course he should know if use your approach, semicolon cannot appear in
> element. He must define, what is an element before want solution of the
> problem.

Indeed, if he knew what an element was it would be better to match it
instead of the delimiter if one would only be interested in the count:

var count = (varArray.match(/[^;]+/g) || "").length;

Or, if an element could contain a field separator but needed to be delimited
by double quotes then:

var count = (varArray.match(/"[^"]*"|[^;]+/g) || "").length;

If double quotes could occur in the value, too, but needed to be escaped:

var count = (varArray.match(/"([^"\\]|\\")*"|[^;]+/g) || "").length;


PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16